ddoronin

ddoronin

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

Showing Posts 1 to 10

thelastinuit

thelastinuit

You can create custom typespec and document with this

msimonborg

msimonborg

The convention is to create a type called t using the @type attribute.

defmodule MyApp.Foo do
  
  defstruct [:foo, :bar]

  @typedoc "My Foo data type."
  @type t :: %__MODULE__{foo: String.t(), bar: String.t()}

  @spec new_foo(keyword) :: t
  def new_foo(opts) do
    struct!(__MODULE__, opts)
  end
end

You can reference your new type from other modules with MyApp.Foo.t() just as you would String.t().

Typespecs

Defstruct#types

ddoronin

ddoronin OP

typedstruct will generate the t() under the hood. My question was more about individual fields on a struct. Those are :foo and :bar in my example:

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

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

What you are suggesting is something like this:

@typedoc "the foo field is so foo"
@type foo::String

@typedoc "the bar field is so bar"
@type bar::String

typedstruct do
  @typedoc "Foo struct is so foo"

  field :foo, foo
  field :bar, bar
end

Obviously I don’t want to create a type for every single field just to document it, because I have dozens of fields and hundreds of structs.

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

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.

sbuttgereit

sbuttgereit

I should note that this assumes this is for public functions/modules. When the code being documented is private, I’ll just use normal code comments to include any helpful documentation with the understanding that won’t render in ExDoc’s output.

msimonborg

msimonborg

btw, this was not what I was suggesting, my original suggestion is outlined in my second comment, but this is a reasonable approach if you want thorough documentation for your fields which may be themselves complex types. It is probably overkill if your fields are built-in types

ddoronin

ddoronin OP

yep, this is for public modules, and I’m using ExDoc.

ddoronin

ddoronin OP

@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.

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)

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
apz
I’m new to elixir and just tried to install the elixirLS extension for VScode(ium) and it is throwing some errors that I would like help ...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews