evnu
Documenting defstruct fields
I am wondering how to document fields in defstruct properly. The convention seems to be to list fields in @moduledoc, but I find that cumbersome. With that approach, I have to jump between @moduledoc and defstruct while reading code. On the other hand, when documenting a field inline with a comment, the field is not documented in the HTML documentation without doubling the comment.
Is there a way to document a field inline, while adding that documentation to @moduledoc as well? I thought that something along the following would be nice, but this fails to parse:
defstruct [
field: :default @fielddoc "my field"
]
Most Liked
slashdotdash
The typed_struct library can be used to alleviate some of the boilerplate.
Example below from the docs:
defmodule Person do
@moduledoc """
A struct representing a person.
"""
use TypedStruct
@typedoc "A person"
typedstruct do
field :name, String.t(), enforce: true
field :age, non_neg_integer()
field :happy?, boolean(), default: true
field :phone, String.t()
end
end
benwilson512
By way of example:
defstruct [
:name
]
@typedoc """
Yay docs
"""
@type t :: %__MODULE__{
name: String.t | nil,
age: age,
}
@typedoc """
You can create named types if you need to comment on the type of a field.
"""
@type age :: pos_integer | nil
LostKobrakai
I always add a typespec for @type t :: … if the struct is of importance and there you’ve got @typedoc.
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex










