Skip to content

Conversation

ice-cap0
Copy link
Contributor

Adds missing documentation for Kernel.struct/2 and warnings about breaking structs with Map functions.

Changes:

  • New "Merging data" section covering Kernel.struct/2 usage
  • New "Broken structs" section warning against Map.put/3 and Map.merge/2
  • Improved pattern matching examples with function heads
  • Better section organization and proper doc links

Why: The current docs were missing Kernel.struct/2 which is crucial for merging data.

## Defining structs

To define a struct, the `defstruct/1` construct is used:
To define a struct, the [`defstruct/1`](https://hexdocs.pm/elixir/Kernel.html#defstruct/1) construct is used:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would say that this is bug in ExDoc if it is not autodiscovered. But IMHO that should not use absolute link to HexDocs. I think that there is some other way to cross-link, but someone more familiar with ExDoc need to point that out.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We do autolink and it works currently as is: https://hexdocs.pm/elixir/structs.html

@josevalim
Copy link
Member

Hi @ice-cap0, this is part of the getting started guide. it is meant to be a gentle introduction rather than a complete reference. Could you instead move this to Kernel.defstruct/1 docs? Thanks!

@ice-cap0
Copy link
Contributor Author

ice-cap0 commented Jul 30, 2025

Gonna give a lil pushback here to argue that these things should be mentioned in Getting Started because:

  1. Structs can be broken and doesn't seem to be addressed anywhere
  2. Kernel.struct/2's raison-d'être is Structs
  3. Safely merging enumerable into a struct is only possible with Kernel.struct/2. Map.merge/2 mentions this but imo it's not very practical to expect beginners learning about Structs to have to discover Kernel.struct/2 through Map.merge/2
  4. Structs not being a module per se have only 1 dedicated page and it is in Getting Started, beginners have nowhere else to discover these things

@josevalim
Copy link
Member

Perhaps we can add a section about dynamically updating structs, using struct (or actually, even better, talk about struct! instead), and then we link to the docs, and the docs have more explanation and context?

```elixir
iex> john = %User{name: "John", age: 27}
%User{age: 27, name: "John"}
iex> struct!(john, name: "Jane", age: 30)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like this example is kind of wrong, you should probably use the map update syntax when you know exactly which fields are going to be updated, like in this case.

Suggested change
iex> struct!(john, name: "Jane", age: 30)
iex> %{john | name: "Jane", age: 30}

The second example seems more correct to me because the update comes from a variable.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good point. Also, data from external sources typically means string keys and may require casting, which isn't tackled by structs by default.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I agree with both points. Will update. I personally just discovered you can use struct/2 to update structs yesterday after like 5 years doing Elixir :D

@josevalim josevalim merged commit 06ff083 into elixir-lang:main Aug 4, 2025
1 check passed
@josevalim
Copy link
Member

💚 💙 💜 💛 ❤️

josevalim pushed a commit that referenced this pull request Aug 4, 2025
ggVGc pushed a commit to ggVGc/elixir-verbatim that referenced this pull request Sep 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

4 participants