ddoronin

ddoronin

How to document fields on a struct?

Hey folks! I want to add documentation for individual fields on a struct/typedstruct, and I’m wondering about available options? I’m new in elixir.

typedstruct do
  @typedoc "Foo struct is so foo"

  # the foo field is so foo
  field :foo, String.t()

  # the bar field is so bar
  field :bar, String.t()
end

Marked As Solved

sbuttgereit

sbuttgereit

Assuming that you’re creating documentation with ExDoc vs. trying to create simple code comments that describe the field I’ll document the individual fields directly in the struct’s @typedoc. So your:

becomes something like:

@typedoc """
Foo struct is so foo

## Fields
  * `:foo` - this is doc for foo.

  * `:bar` - this is doc for bar.
"""

To my knowledge, there’s no great way to document the fields individually that will render in something like ExDoc or iex h like you might expect/want.

Also Liked

msimonborg

msimonborg

No worries :slight_smile: It’s also the same documentation approach for function options and the like

@doc """
Make a new Foo.

## Options

  * `:foo` - A string that is so foo.
  * `:bar` - A string that is so bar.
"""
@spec new_foo(options :: keyword) :: t
def new_foo(options), do: struct!(__MODULE__, options)
msimonborg

msimonborg

Sorry I guess I misunderstood your question! I’m not familiar with that library, I thought it was pseudocode :slight_smile:

IMO, t() is self-documenting as far as what types you expect in the fields. If I wanted more documentation I would add it to the top-level @typedoc. e.g.

@typedoc """
The Foo type.

## Fields

  * `:foo` - A string that says "foo"
  * `:bar` - A string that says "bar"
"""

Maybe there’s a feature in the typedstruct library, but that’s what I would do in vanilla Elixir

ddoronin

ddoronin

@msimonborg Gotcha! It was probably mixed up in my head with another answer given by thelastinuit. It seems like @typedoc with the “Fields” section is the most common way to document struct fields.

ddoronin

ddoronin

nice! this is really helpful!

Where Next?

Popular in Questions Top

itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
idi527
I’ve been re-reading swift book again and noticed that multiline strings there don’t have a trailing line break, unlike in elixir iex(2)...
New

Other popular topics Top

Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3268 119930 1237
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
977 41022 311
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
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
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
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New

We're in Beta

About us Mission Statement