# \#defstruct

**URL:** https://forum.elixirforum.com/tag/defstruct/1009.md

[Latest](https://forum.elixirforum.com/latest.md) · [Categories](https://forum.elixirforum.com/categories.md) · [Tags](https://forum.elixirforum.com/tags.md)

---

## [How do you go around structs ideally having less than 32 fields?](https://forum.elixirforum.com/t/how-do-you-go-around-structs-ideally-having-less-than-32-fields/72225)

<div class="topic-metadata">

**Author:** [@ryanzidago](https://forum.elixirforum.com/u/ryanzidago)\
**Replies:** 3\
**Last updated:** [August 25, 2025, 1:28pm UTC](https://forum.elixirforum.com/t/how-do-you-go-around-structs-ideally-having-less-than-32-fields/72225 "2025-08-25T13:28:42Z")

</div>

In the official docs, it’s written that ideally structs should have less than 32 fields (source here). Do you go around that limitation? If yes, how? It’s very easy for an ecto schema to have more than 32 fields, espec…

---

## [What's the best way to define a module-local struct?](https://forum.elixirforum.com/t/whats-the-best-way-to-define-a-module-local-struct/65833)

<div class="topic-metadata">

**Author:** [@budgie](https://forum.elixirforum.com/u/budgie)\
**Replies:** 4\
**Last updated:** [September 3, 2024, 12:40am UTC](https://forum.elixirforum.com/t/whats-the-best-way-to-define-a-module-local-struct/65833 "2024-09-03T00:40:07Z")

</div>

What’s the best/most succinct way to define a struct that is only used inside of one module? I sometimes have structures that I want to be more explicit than a map but that have no use elsewhere. Is there an accepted so…

---

## [Customer.\_\_struct\_\_/1 is undefined, cannot expand Customer](https://forum.elixirforum.com/t/customer-struct-1-is-undefined-cannot-expand-customer/64244)

<div class="topic-metadata">

**Author:** [@holasoymas](https://forum.elixirforum.com/u/holasoymas)\
**Replies:** 3\
**Last updated:** [June 16, 2024, 5:00pm UTC](https://forum.elixirforum.com/t/customer-struct-1-is-undefined-cannot-expand-customer/64244 "2024-06-16T17:00:34Z")

</div>

nested\_dict.exs defmodule Customer do defstruct name: "", company: "" end defmodule BugReport do defstruct owner: %Customer{}, details: "", severity: 1 end I got this error after writing this in iex iex(1)\> c "…

---

## [Is it possible to define structs at runtime](https://forum.elixirforum.com/t/is-it-possible-to-define-structs-at-runtime/62537)

<div class="topic-metadata">

**Author:** [@chazwatkins](https://forum.elixirforum.com/u/chazwatkins)\
**Replies:** 14\
**Last updated:** [April 4, 2024, 1:53pm UTC](https://forum.elixirforum.com/t/is-it-possible-to-define-structs-at-runtime/62537 "2024-04-04T13:53:01Z")

</div>

I’m considering writing an integration with Salesforce. Salesforce has SObjects of various types like Case, Quote, Lead, CustomObject\_\_c, etc. These SObjects are like database tables with their own schemas. I’d like t…

---

## [Get types from struct in compilation time](https://forum.elixirforum.com/t/get-types-from-struct-in-compilation-time/55475)

<div class="topic-metadata">

**Author:** [@ntd23](https://forum.elixirforum.com/u/ntd23)\
**Replies:** 2\
**Last updated:** [April 24, 2023, 2:26pm UTC](https://forum.elixirforum.com/t/get-types-from-struct-in-compilation-time/55475 "2023-04-24T14:26:29Z")

</div>

Is there way to get the types from struct defined as bellow, and generate functions based on your types; like binary(), integer()? Can I get the keys of a struct that is marked as “enforced”? So I have a struct c…

---

## [Struct without fields](https://forum.elixirforum.com/t/struct-without-fields/39799)

<div class="topic-metadata">

**Author:** [@Sebb](https://forum.elixirforum.com/u/Sebb)\
**Replies:** 7\
**Last updated:** [March 31, 2023, 7:33pm UTC](https://forum.elixirforum.com/t/struct-without-fields/39799 "2023-03-31T19:33:59Z")

</div>

I just tried to create a struct without fields (because: why not?). It’s not possible with defstruct, which insists on an argument. Best it can do is defstruct \_: nil # nice btw, this is not OK: defstruct \[\_\] isn’t…

---

## [Composing Types with TypedStruct?](https://forum.elixirforum.com/t/composing-types-with-typedstruct/54450)

<div class="topic-metadata">

**Author:** [@Fl4m3Ph03n1x](https://forum.elixirforum.com/u/Fl4m3Ph03n1x)\
**Replies:** 6\
**Last updated:** [March 10, 2023, 9:14am UTC](https://forum.elixirforum.com/t/composing-types-with-typedstruct/54450 "2023-03-10T09:14:36Z")

</div>

Background Let’s say I have a TypedStruct that has some common fields: typedstruct do @typedoc "A product." field(:item\_id, String.t()) field(:name, String.t()) field(:brand, String.t()) end This is …

---

## [Renaming a struct (or how to achieve "strong aliasing"?)](https://forum.elixirforum.com/t/renaming-a-struct-or-how-to-achieve-strong-aliasing/52339)

<div class="topic-metadata">

**Author:** [@ahamez](https://forum.elixirforum.com/u/ahamez)\
**Replies:** 0\
**Last updated:** [December 7, 2022, 7:40pm UTC](https://forum.elixirforum.com/t/renaming-a-struct-or-how-to-achieve-strong-aliasing/52339 "2022-12-07T19:40:47Z")

</div>

Hi all, Let’s suppose I have the following struct: defmodule DataType do defstruct ~w(a b c d e f)a end This struct is parsed from a byte stream and represents a generic data type. However, different messages can be…

---

## [What is the most idiomatic way to construct structs with complex default values?](https://forum.elixirforum.com/t/what-is-the-most-idiomatic-way-to-construct-structs-with-complex-default-values/48727)

<div class="topic-metadata">

**Author:** [@elt547](https://forum.elixirforum.com/u/elt547)\
**Replies:** 20\
**Last updated:** [July 29, 2022, 2:39am UTC](https://forum.elixirforum.com/t/what-is-the-most-idiomatic-way-to-construct-structs-with-complex-default-values/48727 "2022-07-29T02:39:19Z")

</div>

I have a struct representing a session request with only two fields, email and bytes. Basically the struct should either be passed around as immutable or created with a default value as a function return value. I have a …

---

## [Load json into struct in module-attribute](https://forum.elixirforum.com/t/load-json-into-struct-in-module-attribute/43084)

<div class="topic-metadata">

**Author:** [@Sebb](https://forum.elixirforum.com/u/Sebb)\
**Replies:** 5\
**Last updated:** [October 14, 2021, 2:26pm UTC](https://forum.elixirforum.com/t/load-json-into-struct-in-module-attribute/43084 "2021-10-14T14:26:32Z")

</div>

I want to load a json (list of objects) into list of structs in a module attribute. Seems like I need three modules for that. json \[ {"foo": 1, "bar": 2}, {"foo": 11, "bar": 22} \] struct defmodule Foo do d…

---

## [Why can you remove fields from defstructs's maps if you can't add them?](https://forum.elixirforum.com/t/why-can-you-remove-fields-from-defstructss-maps-if-you-cant-add-them/38372)

<div class="topic-metadata">

**Author:** [@tadiou](https://forum.elixirforum.com/u/tadiou)\
**Replies:** 10\
**Last updated:** [March 24, 2021, 6:09pm UTC](https://forum.elixirforum.com/t/why-can-you-remove-fields-from-defstructss-maps-if-you-cant-add-them/38372 "2021-03-24T18:09:49Z")

</div>

I was just going through some stuff with some jr. devs about how structs work, and we were fooling around with what can be done with them as an exercise, and then… well. We struct (lol) something I wasn’t able to explain…

---

## [Is it possible to declare a generic type?](https://forum.elixirforum.com/t/is-it-possible-to-declare-a-generic-type/19209)

<div class="topic-metadata">

**Author:** [@lessless](https://forum.elixirforum.com/u/lessless)\
**Replies:** 34\
**Last updated:** [April 12, 2020, 6:10pm UTC](https://forum.elixirforum.com/t/is-it-possible-to-declare-a-generic-type/19209 "2020-04-12T18:10:40Z")

</div>

For example I have multiple currencies dollar.ex defmodule Multicurrency.Currency.Dollar do @enforce\_keys \[:amount\] defstruct \[:amount\] @type t :: %\_\_MODULE\_\_{ amount: integer() } @spec new(number()) ::…

---

## [Dialyzer Unknown type for struct](https://forum.elixirforum.com/t/dialyzer-unknown-type-for-struct/28336)

<div class="topic-metadata">

**Author:** [@Fl4m3Ph03n1x](https://forum.elixirforum.com/u/Fl4m3Ph03n1x)\
**Replies:** 3\
**Last updated:** [January 17, 2020, 2:59pm UTC](https://forum.elixirforum.com/t/dialyzer-unknown-type-for-struct/28336 "2020-01-17T14:59:59Z")

</div>

Background I have a custom struct and I want to define a type for it so I can run Dialyzer checks. However Dialyzer is complaining that I have an unknown type. Code I have a struct that defines an Event and I have defin…

---

## [Elixir allows Map functions to manipulate structs](https://forum.elixirforum.com/t/elixir-allows-map-functions-to-manipulate-structs/23826)

<div class="topic-metadata">

**Author:** [@brettp](https://forum.elixirforum.com/u/brettp)\
**Replies:** 14\
**Last updated:** [September 19, 2019, 5:05pm UTC](https://forum.elixirforum.com/t/elixir-allows-map-functions-to-manipulate-structs/23826 "2019-09-19T17:05:43Z")

</div>

We ran into an issue today with structs that I found a bit surprising. The essence of the issue is: A struct is a Map under the hood you can manipulate a struct with Map functions, deleting keys that should be there a…

---

## [Can I have a devstruct with computed values?](https://forum.elixirforum.com/t/can-i-have-a-devstruct-with-computed-values/19551)

<div class="topic-metadata">

**Author:** [@mxst](https://forum.elixirforum.com/u/mxst)\
**Replies:** 3\
**Last updated:** [January 20, 2019, 4:57pm UTC](https://forum.elixirforum.com/t/can-i-have-a-devstruct-with-computed-values/19551 "2019-01-20T16:57:49Z")

</div>

Hey everyone. I was wondering about how I should initialize a struct where I have some values and some computed values. For now I came up with: defmodule Stats do defstruct annotations: 0, true\_positive: 0, fals…

---

## [Documenting defstruct fields](https://forum.elixirforum.com/t/documenting-defstruct-fields/10295)

<div class="topic-metadata">

**Author:** [@evnu](https://forum.elixirforum.com/u/evnu)\
**Replies:** 4\
**Last updated:** [August 28, 2018, 3:31pm UTC](https://forum.elixirforum.com/t/documenting-defstruct-fields/10295 "2018-08-28T15:31:24Z")

</div>

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 …
