# \#struct

**URL:** https://forum.elixirforum.com/tag/struct/571.md

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

---

## [Typestruct 0.6.0 released - Added Ecto Integration, Default Plugins, and Enhanced Struct Features](https://forum.elixirforum.com/t/typestruct-0-6-0-released-added-ecto-integration-default-plugins-and-enhanced-struct-features/77308)

<div class="topic-metadata">

**Author:** [@saleyn](https://forum.elixirforum.com/u/saleyn)\
**Replies:** 1\
**Last updated:** [November 8, 2026, 3:36am UTC](https://forum.elixirforum.com/t/typestruct-0-6-0-released-added-ecto-integration-default-plugins-and-enhanced-struct-features/77308 "2026-11-08T03:36:44Z")

</div>

TypedStruct 0.6.2: Added Ecto Integration, Default Plugins, and Enhanced Struct Features I wanted to share some exciting updates to TypedStruct that have landed recently, bringing significant improvements to struct defin…

---

## [Draft - enforce typed structs and validation rules with draft schema](https://forum.elixirforum.com/t/draft-enforce-typed-structs-and-validation-rules-with-draft-schema/74631)

<div class="topic-metadata">

**Author:** [@2p4b](https://forum.elixirforum.com/u/2p4b)\
**Replies:** 2\
**Last updated:** [March 25, 2026, 6:17am UTC](https://forum.elixirforum.com/t/draft-enforce-typed-structs-and-validation-rules-with-draft-schema/74631 "2026-03-25T06:17:08Z")

</div>

Define typed structs with built-in validation. Draft allows developers to describe the structure of data and enforce constraints when constructing structs, helping ensure that invalid data never enters your domain. Inst…

---

## [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…

---

## [Tests as First-Class Citizens](https://forum.elixirforum.com/t/tests-as-first-class-citizens/70871)

<div class="topic-metadata">

**Author:** [@mudasobwa](https://forum.elixirforum.com/u/mudasobwa)\
**Replies:** 0\
**Last updated:** [May 14, 2025, 12:30pm UTC](https://forum.elixirforum.com/t/tests-as-first-class-citizens/70871 "2025-05-14T12:30:14Z")

</div>

---

## [Want - Type Conversion and Coercion Library](https://forum.elixirforum.com/t/want-type-conversion-and-coercion-library/67614)

<div class="topic-metadata">

**Author:** [@wrren](https://forum.elixirforum.com/u/wrren)\
**Replies:** 8\
**Last updated:** [November 22, 2024, 11:36am UTC](https://forum.elixirforum.com/t/want-type-conversion-and-coercion-library/67614 "2024-11-22T11:36:13Z")

</div>

I wrote this library several years ago, but I’ve recently had a use case for it that’s caused me to make several updates that I think would also make it quite useful for others. Initially, I wanted a library that let me …

---

## [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…

---

## [How to have custom enconding for struct using Jason?](https://forum.elixirforum.com/t/how-to-have-custom-enconding-for-struct-using-jason/46526)

<div class="topic-metadata">

**Author:** [@Fl4m3Ph03n1x](https://forum.elixirforum.com/u/Fl4m3Ph03n1x)\
**Replies:** 14\
**Last updated:** [July 20, 2024, 4:58pm UTC](https://forum.elixirforum.com/t/how-to-have-custom-enconding-for-struct-using-jason/46526 "2024-07-20T16:58:38Z")

</div>

Background I am trying to encode a structure into json format using the Jason library. However, this is not working as expected. Code Let’s assume I have this struct: defmodule Test do defstruct \[:foo, :bar, :baz\] e…

---

## [Inspect/2 output for structs: executable code or structure?](https://forum.elixirforum.com/t/inspect-2-output-for-structs-executable-code-or-structure/64659)

<div class="topic-metadata">

**Author:** [@kip](https://forum.elixirforum.com/u/kip)\
**Replies:** 14\
**Last updated:** [July 7, 2024, 11:42pm UTC](https://forum.elixirforum.com/t/inspect-2-output-for-structs-executable-code-or-structure/64659 "2024-07-07T23:42:11Z")

</div>

TLDR; What should I emit for inspect/2 output of library structs? Executable code, string representation or default (outputs the struct fields)? Summary In April, I changed the output of inspect/2 for Cldr.LanguageTag.…

---

## [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…

---

## [How to not encode nil field from Struct using Jason](https://forum.elixirforum.com/t/how-to-not-encode-nil-field-from-struct-using-jason/54447)

<div class="topic-metadata">

**Author:** [@Fl4m3Ph03n1x](https://forum.elixirforum.com/u/Fl4m3Ph03n1x)\
**Replies:** 1\
**Last updated:** [March 9, 2023, 3:45pm UTC](https://forum.elixirforum.com/t/how-to-not-encode-nil-field-from-struct-using-jason/54447 "2023-03-09T15:45:32Z")

</div>

Background I have a TypedStruct with an optional field, :rank. This field is sometimes present and sometimes not. @derive Jason.Encoder typedstruct enforce: true do @typedoc "An order." field(:item\_id, Stri…

---

## [Struct Data Transformations - best approach to simple data transformations?](https://forum.elixirforum.com/t/struct-data-transformations-best-approach-to-simple-data-transformations/52616)

<div class="topic-metadata">

**Author:** [@RoboZoom](https://forum.elixirforum.com/u/RoboZoom)\
**Replies:** 2\
**Last updated:** [December 18, 2022, 7:04pm UTC](https://forum.elixirforum.com/t/struct-data-transformations-best-approach-to-simple-data-transformations/52616 "2022-12-18T19:04:47Z")

</div>

New to elixir here - looking for help for the best approach to simple data transformations. I have a config file that I need to transform slightly into a slightly different format to fit a service call. In essence, it …

---

## [How to document fields on a struct?](https://forum.elixirforum.com/t/how-to-document-fields-on-a-struct/48676)

<div class="topic-metadata">

**Author:** [@ddoronin](https://forum.elixirforum.com/u/ddoronin)\
**Replies:** 11\
**Last updated:** [June 29, 2022, 10:18pm UTC](https://forum.elixirforum.com/t/how-to-document-fields-on-a-struct/48676 "2022-06-29T22:18:39Z")

</div>

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 …

---

## [Can a defguard definition match against a struct?](https://forum.elixirforum.com/t/can-a-defguard-definition-match-against-a-struct/23761)

<div class="topic-metadata">

**Author:** [@dimitarvp](https://forum.elixirforum.com/u/dimitarvp)\
**Replies:** 14\
**Last updated:** [June 21, 2022, 8:33pm UTC](https://forum.elixirforum.com/t/can-a-defguard-definition-match-against-a-struct/23761 "2022-06-21T20:33:59Z")

</div>

Hello, Is it possible to check if an argument to a guard is a particular structure? Context: I am trying to have a defguard definition that checks if something is an Ecto query – which means it can either be a module (…

---

## [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…

---

## [I think Elixir 2.0 should drop structs](https://forum.elixirforum.com/t/i-think-elixir-2-0-should-drop-structs/41766)

<div class="topic-metadata">

**Author:** [@darkmarmot](https://forum.elixirforum.com/u/darkmarmot)\
**Replies:** 23\
**Last updated:** [August 22, 2021, 10:29am UTC](https://forum.elixirforum.com/t/i-think-elixir-2-0-should-drop-structs/41766 "2021-08-22T10:29:06Z")

</div>

Someone recently asked “What feature would you most like removed from your language?” And, for me, it has to be structs or the way Elixir currently handles them. In my job, we do a lot of deployments on distributed clu…

---

## [Load struct given its name from string](https://forum.elixirforum.com/t/load-struct-given-its-name-from-string/40641)

<div class="topic-metadata">

**Author:** [@superruzafa](https://forum.elixirforum.com/u/superruzafa)\
**Replies:** 6\
**Last updated:** [June 24, 2021, 12:18pm UTC](https://forum.elixirforum.com/t/load-struct-given-its-name-from-string/40641 "2021-06-24T12:18:50Z")

</div>

Hi! I have a couple of structs and I want to build one of them depending on some STRING as input: defmodule Mystruct1 do ... end defmodule Mystruct2 do ... end ... defmodule Mystructn do ... end def get\_struct(name) d…

---

## [Business logic in Finite State Machine](https://forum.elixirforum.com/t/business-logic-in-finite-state-machine/25215)

<div class="topic-metadata">

**Author:** [@smanza](https://forum.elixirforum.com/u/smanza)\
**Replies:** 1\
**Last updated:** [January 20, 2021, 1:46pm UTC](https://forum.elixirforum.com/t/business-logic-in-finite-state-machine/25215 "2021-01-20T13:46:31Z")

</div>

Hello, i’m new to Elixir and Erlang world, but I want to create mechanism to restrict the use of a module through steps involving data. So I thought using finite state machine, with some states and data. But I need to ex…

---

## [%Struct{} vs %{\_\_struct\_\_: Struct}](https://forum.elixirforum.com/t/struct-vs-struct-struct/36061)

<div class="topic-metadata">

**Author:** [@otuv](https://forum.elixirforum.com/u/otuv)\
**Replies:** 4\
**Last updated:** [December 9, 2020, 12:49am UTC](https://forum.elixirforum.com/t/struct-vs-struct-struct/36061 "2020-12-09T00:49:22Z")

</div>

Hi, Lets say I have a struct, lets say named Struct with the singular field x (default 0). When I create it I get &Struct{x: 0} Since they are maps I then use Map.put to add something else like y: 5 I get %{struct: S,…

---

## [Typespec for any struct with required key](https://forum.elixirforum.com/t/typespec-for-any-struct-with-required-key/35535)

<div class="topic-metadata">

**Author:** [@vinibrl](https://forum.elixirforum.com/u/vinibrl)\
**Replies:** 1\
**Last updated:** [November 13, 2020, 8:47pm UTC](https://forum.elixirforum.com/t/typespec-for-any-struct-with-required-key/35535 "2020-11-13T20:47:43Z")

</div>

I’m looking for a typespec that would match, for instance: %Foo{specific\_key: "abc"} %Bar{specific\_key: "abc"} %Baz{specific\_key: "abc"} I’m using %{specific\_key: String.t()} but that can match maps as well. Is there s…

---

## [Accessing/updating Struct error during Enum.map](https://forum.elixirforum.com/t/accessing-updating-struct-error-during-enum-map/35484)

<div class="topic-metadata">

**Author:** [@Ettu\_R](https://forum.elixirforum.com/u/Ettu_R)\
**Replies:** 3\
**Last updated:** [November 11, 2020, 1:07pm UTC](https://forum.elixirforum.com/t/accessing-updating-struct-error-during-enum-map/35484 "2020-11-11T13:07:19Z")

</div>

I have a struct define as follows defmodule Employee do defstruct \[:fname, :lname, :id, salary: 0, job: "none"\] then I made a list, consisting of Employee structs and added one in there. At some point I want to searc…

---

## [Strategy for building complicated struct - passing through functions / building once?](https://forum.elixirforum.com/t/strategy-for-building-complicated-struct-passing-through-functions-building-once/34218)

<div class="topic-metadata">

**Author:** [@benhoven](https://forum.elixirforum.com/u/benhoven)\
**Replies:** 6\
**Last updated:** [September 7, 2020, 9:02pm UTC](https://forum.elixirforum.com/t/strategy-for-building-complicated-struct-passing-through-functions-building-once/34218 "2020-09-07T21:02:53Z")

</div>

Hi All, I didn’t find any guides / recommendations for Elixir about the correct architectural design / strategy for building complicated structs. Could you please advise? I’m working on an app that shows current status…

---

## [Struct and complex logic](https://forum.elixirforum.com/t/struct-and-complex-logic/32449)

<div class="topic-metadata">

**Author:** [@smanza](https://forum.elixirforum.com/u/smanza)\
**Replies:** 7\
**Last updated:** [June 21, 2020, 7:13pm UTC](https://forum.elixirforum.com/t/struct-and-complex-logic/32449 "2020-06-21T19:13:23Z")

</div>

Hello. I practiced Elixir for few months now but every time I wonder about some code organisation and particularly with struct and business logic. I don’t know if struct must only define simple data transformation and …

---

## [How to suppress struct name expansion?](https://forum.elixirforum.com/t/how-to-suppress-struct-name-expansion/26109)

<div class="topic-metadata">

**Author:** [@Oliver](https://forum.elixirforum.com/u/Oliver)\
**Replies:** 6\
**Last updated:** [October 16, 2019, 7:51pm UTC](https://forum.elixirforum.com/t/how-to-suppress-struct-name-expansion/26109 "2019-10-16T19:51:27Z")

</div>

I’m generating a module from a macro, like this: defmodule WhereTheMacroIs do defstruct \[:same\_for\_all\] defmacro generate(name) do quote do defmodule unquote(name) do def simplified\_example(%WhereTheMacroI…

---

## [Pyc → Structs on Steroids](https://forum.elixirforum.com/t/pyc-structs-on-steroids/20086)

<div class="topic-metadata">

**Author:** [@mudasobwa](https://forum.elixirforum.com/u/mudasobwa)\
**Replies:** 7\
**Last updated:** [February 11, 2019, 11:30pm UTC](https://forum.elixirforum.com/t/pyc-structs-on-steroids/20086 "2019-02-11T23:30:26Z")

</div>

Blog post, announcing Pyc package. It allows transparent validation across inserted data with Exvalibur, chaining functions in a monadic-like way, and exposes all the keys as local variables inside methods declared with d…

---

## [How to take out a value from a list of structs](https://forum.elixirforum.com/t/how-to-take-out-a-value-from-a-list-of-structs/20083)

<div class="topic-metadata">

**Author:** [@benonymus](https://forum.elixirforum.com/u/benonymus)\
**Replies:** 5\
**Last updated:** [February 11, 2019, 11:04am UTC](https://forum.elixirforum.com/t/how-to-take-out-a-value-from-a-list-of-structs/20083 "2019-02-11T11:04:27Z")

</div>

Hey I have a list of structs and it has fields like: id, date, counter etc how would i take out every id from this list and return the ids as a new list?

---

## [Storing list of structs in file one by one](https://forum.elixirforum.com/t/storing-list-of-structs-in-file-one-by-one/19683)

<div class="topic-metadata">

**Author:** [@dalhorinek](https://forum.elixirforum.com/u/dalhorinek)\
**Replies:** 2\
**Last updated:** [January 25, 2019, 8:50pm UTC](https://forum.elixirforum.com/t/storing-list-of-structs-in-file-one-by-one/19683 "2019-01-25T20:50:15Z")

</div>

Hello, I’m having a list of elixir structs and I need to store in into file for later use and I don’t want to serialise it. So I used :erlang.term\_to\_binary on the list and that worked fine, but sometimes it happens th…

---

## [Remove fields from nested struct](https://forum.elixirforum.com/t/remove-fields-from-nested-struct/16373)

<div class="topic-metadata">

**Author:** [@script](https://forum.elixirforum.com/u/script)\
**Replies:** 11\
**Last updated:** [September 7, 2018, 1:50pm UTC](https://forum.elixirforum.com/t/remove-fields-from-nested-struct/16373 "2018-09-07T13:50:45Z")

</div>

I have this struct of preload records. I want to remove \_\_meta\_\_ and Ecto.Association.NotLoaded from the main struct as well as from the nested array of preloaded records. To make it a simple map %Facility{ \_\_met…

---

## [Best way to avoid duplication in struct definition?](https://forum.elixirforum.com/t/best-way-to-avoid-duplication-in-struct-definition/14626)

<div class="topic-metadata">

**Author:** [@sdia](https://forum.elixirforum.com/u/sdia)\
**Replies:** 2\
**Last updated:** [June 9, 2018, 9:23pm UTC](https://forum.elixirforum.com/t/best-way-to-avoid-duplication-in-struct-definition/14626 "2018-06-09T21:23:43Z")

</div>

As the title says, can someone advises on the best way to remove the duplication in the code snippet below. I am referring to the field list. By the way is there a way to retrieve that field list from any struct - did n…

---

## [How to get struct in array](https://forum.elixirforum.com/t/how-to-get-struct-in-array/13993)

<div class="topic-metadata">

**Author:** [@shahryarjb](https://forum.elixirforum.com/u/shahryarjb)\
**Replies:** 13\
**Last updated:** [May 3, 2018, 10:15am UTC](https://forum.elixirforum.com/t/how-to-get-struct-in-array/13993 "2018-05-03T10:15:05Z")

</div>

Hello, how do I get struct in array ? please see my pic : I need to get the struct’s params , like this : a.cms\_post.title My output is : \[ %TrangellCmsService.Cms.Db.Post{ \_\_meta\_\_: #Ecto.Schema.Metadata\<:l…
