Possible method call error -> docstring above struct fields

In VS Code, I keep getting that the linter returns a “possible method call error” if I try to create an instance of a struct with fields to which I’ve added docstrings; see the below MWE where the linter doesn’t complain about Foo1 but does complain about Foo2. Is the only way to avoid this to put the full docstring above the definition of the struct (Foo3), or is there some easy way of doing this? In my project I have structs with roughly 10 fields and I would like to keep the relevant descriptions close to their fields for easy reference.

struct Foo1{T<:Real}
    x::T
end
struct Foo2{T<:Real}
    """Same as above, but with a docstring"""
    x::T
end

"""Same as above, but with a docstring above the struct"""
struct Foo3{T<:Real}
    x::T
end

function Test()
    Foo1(1)
    Foo2(1) # Triggers the linter
    #Foo2(1, 1) doesn't trigger the linter (but is not a valid call!)
    Foo3(1)
end

Test()

Two images to show the behaviour I mean

I think the idea of a docstring is that it is one bit of text that describes the defined object independently of that definition itself (e.g. in docs or requested via the REPL). For structs fieldnames are often repeated in the docstring. You could repeat the field descriptions in comments next to the field definitions if you want.

On the other hand, you could probably also define a macro that collects the field descriptions into a single docstring which is put above the definition (if such a thing does not already exist, I didn’t find it with a quick Google search).

1 Like

There’s this: [ANN] AutomaticDocstrings.jl - Package Announcements - Julia Programming Language

1 Like

Thank you, repeating the descriptions in comments seems like a good solution for me!

1 Like