evnu

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

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

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

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

LostKobrakai

I always add a typespec for @type t :: … if the struct is of importance and there you’ve got @typedoc.

Where Next?

Popular in Questions Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New

Other popular topics Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 44167 214
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New

We're in Beta

About us Mission Statement