-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Update structs.md #14683
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update structs.md #14683
Conversation
## 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: |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
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 |
Gonna give a lil pushback here to argue that these things should be mentioned in Getting Started because:
|
Perhaps we can add a section about dynamically updating structs, using |
```elixir | ||
iex> john = %User{name: "John", age: 27} | ||
%User{age: 27, name: "John"} | ||
iex> struct!(john, name: "Jane", age: 30) |
There was a problem hiding this comment.
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.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
💚 💙 💜 💛 ❤️ |
Adds missing documentation for
Kernel.struct/2
and warnings about breaking structs withMap
functions.Changes:
Kernel.struct/2
usageMap.put/3
andMap.merge/2
Why: The current docs were missing
Kernel.struct/2
which is crucial for merging data.