Accessing docstring of a field

Fields of a struct can be documented, but how does one retrieve the docstrings? Is it possible via ? in the REPL? MWE:

struct Foo
    "I want this docstring"
    x
end
1 Like
julia> "docstring for T type"
       struct T
           "docstring for x field"
           x
           "docstring for y field"
           y
       end
T

help?> T
search: try Type Task toq toc tic tan Text Test Tuple triu tril time tanh tand Timer tuple throw trunc trues triu! tril! trace touch take!

  docstring for T type

help?> T.x
  docstring for x field

help?> T.y
  docstring for y field
4 Likes

Looks like it only shows up if you also have docstrings for the struct itself

julia> """
        Foo docs
        """struct Foo
            "I want this docstring"
            x
        end
Foo

help?> Foo.x
  I want this docstring
1 Like

What if the type is parametric?

julia> "..."
       struct T{X,Y}
           "x"
           x::X
           "y"
           y::Y
       end
T

help?> T
search: T try Type Task tan Text Tuple time tanh tand Timer tuple throw trunc trues touch take! typeof TypeVar Threads typemin typemax trylock time_ns thisind tempdir TypeError typejoin tryparse truncate

  ...

help?> T.x
ERROR: MethodError: no method matching Base.Docs.Binding(::Type{T}, ::Symbol)
Closest candidates are:
  Base.Docs.Binding(::Module, ::Symbol) at docs/bindings.jl:12
Stacktrace:
 [1] top-level scope at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.1/REPL/src/docview.jl:355

There is an open issue for this:

https://github.com/JuliaLang/julia/issues/29478

1 Like

I am reviving an old thread but I think the subject is very relevant.

Is there a way to access the field docstrings programmatically without using the interactive help in the REPL?

I can access the basic documentation of the struct either with Main.Docs.Binding(Main, :T) or with @doc T but I didn’t find out how to extract the docstring for the field T.x programmatically

1 Like

I finally got some time to try and search for this a bit more in detail.

The function for accessing the docstring of a field is called fielddoc and is inside the REPL module:

2 Likes

I found this thread looking to see if there is a way to get VS Code to show the field docstring when hovering on a field. From the above, it seems like it should be feasible. Does anyone know if it can be enabled?