# How to document type properties defined with \`getproperty\`?

**URL:** <https://discourse.julialang.org/t/how-to-document-type-properties-defined-with-getproperty/31174>\
**Category:** General Usage\
**Tags:** documentation\
**Created:** [November 17, 2019, 10:55am UTC](https://discourse.julialang.org/t/how-to-document-type-properties-defined-with-getproperty/31174 "2019-11-17T10:55:50Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![fverdugo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fverdugo/32/9446_2.png) [@fverdugo](https://discourse.julialang.org/u/fverdugo)\
**Post date:** [November 17, 2019, 10:55am UTC](https://discourse.julialang.org/t/how-to-document-type-properties-defined-with-getproperty/31174/1 "2019-11-17T10:55:50Z")

</div>

Consider the structs `Foo` and `Bar`:

```julia
"""
Doc for type `Foo`.
"""
struct Foo
  """
  Doc for field Foo.x
  """
  x::Int
  Foo() = new(2)
end

"""
Doc for type `Bar`.
"""
struct Bar end

Base.propertynames(::Bar) = (:x,)

function Base.getproperty(a::Bar,s::Symbol)
  if s == :x
    2
  else
    error("type Bar has no field $s")
  end
end

function Base.show(io::IO,::Bar)
  print(io,"Bar(2)")
end
 

```

They have an (almost) identical API

```julia
julia> foo = Foo()
Foo(2)

julia> bar = Bar()
Bar(2)

julia> foo.x
2

julia> bar.x
2

```

Except for documentation

```julia
help?> Foo.x
  Doc for field Foo.x

help?> Bar.x
  Bar has no fields.

```

Is there any way of documenting the property `Bar.x` ?

Thanks for the help!

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [November 17, 2019, 12:31pm UTC](https://discourse.julialang.org/t/how-to-document-type-properties-defined-with-getproperty/31174/2 "2019-11-17T12:31:36Z")

</div>

This is currently an open question, and there is no solution for it that would replicate the convenience of the field docstring. Cf

[https://github.com/JuliaLang/julia/issues/27119](https://github.com/JuliaLang/julia/issues/27119)

I would suggest documenting valid properties in the docstring of `Foo`, or the usual function that users call to make `Foo`.

---

<div class="post-metadata">

**Author:** ![fverdugo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fverdugo/32/9446_2.png) [@fverdugo](https://discourse.julialang.org/u/fverdugo)\
**Post date:** [November 18, 2019, 6:41am UTC](https://discourse.julialang.org/t/how-to-document-type-properties-defined-with-getproperty/31174/3 "2019-11-18T06:41:21Z")

</div>

Thanks for answering!

Another solution I can think of is to create a getter getx(a::Bar) = a.x and document this getter and inform the users of the package that properties are documented in the corresponding getter.

Do you see any problem with this?

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [November 18, 2019, 7:20am UTC](https://discourse.julialang.org/t/how-to-document-type-properties-defined-with-getproperty/31174/4 "2019-11-18T07:20:00Z")

</div>

If there is an explicit accessor function, it is of course good practice to document that.

But one advantage of property accessors is that you don’t have to bother with defining a function for this. How to document that is somewhat of an open question while practice evolves, but at least discoverability is fine thanks to `propertynames`.
