Skip to content

Commit c911466

Browse files
committed
Provide references on structs for more information, closes #13664
1 parent 7890e56 commit c911466

File tree

3 files changed

+22
-18
lines changed

3 files changed

+22
-18
lines changed

lib/elixir/lib/kernel.ex

+9-5
Original file line numberDiff line numberDiff line change
@@ -5326,13 +5326,10 @@ defmodule Kernel do
53265326
53275327
A struct is a tagged map that allows developers to provide
53285328
default values for keys, tags to be used in polymorphic
5329-
dispatches and compile time assertions. For more information
5330-
about structs, please check `%/2`.
5329+
dispatches and compile time assertions.
53315330
53325331
It is only possible to define a struct per module, as the
5333-
struct is tied to the module itself. Calling `defstruct/1`
5334-
also defines a `__struct__/0` function that returns the
5335-
struct itself.
5332+
struct is tied to the module itself.
53365333
53375334
## Examples
53385335
@@ -5366,6 +5363,13 @@ defmodule Kernel do
53665363
defstruct [:title, :content, :author]
53675364
end
53685365
5366+
Once a struct is defined, it is possible to create them as follows:
5367+
5368+
%Post{title: "Hello world!"}
5369+
5370+
For more information on creating, updating, and pattern matching on
5371+
structs, please check `%/2`.
5372+
53695373
## Deriving
53705374
53715375
Although structs are maps, by default structs do not implement

lib/elixir/lib/kernel/special_forms.ex

+11-13
Original file line numberDiff line numberDiff line change
@@ -112,26 +112,24 @@ defmodule Kernel.SpecialForms do
112112
113113
%User{age: age} = user
114114
115-
An update operation specific for structs is also available:
116-
117-
%User{user | age: 28}
118-
119115
The advantage of structs is that they validate that the given
120116
keys are part of the defined struct. The example below will fail
121117
because there is no key `:full_name` in the `User` struct:
122118
123119
%User{full_name: "john doe"}
124120
125-
The syntax above will guarantee the given keys are valid at
126-
compilation time and it will guarantee at runtime the given
127-
argument is a struct, failing with `BadStructError` otherwise.
121+
An update operation specific for structs is also available:
122+
123+
%User{user | age: 28}
124+
125+
Once again, the syntax above will guarantee the given keys
126+
are valid at compilation time and it will guarantee at runtime
127+
the given argument is a struct, failing with `BadStructError`
128+
otherwise. The map update syntax can also be used for updating
129+
structs, and it is useful when you want to update any struct,
130+
regardless of their name, as long as they have matching fields:
128131
129-
Although structs are maps, by default structs do not implement
130-
any of the protocols implemented for maps. Check
131-
`Kernel.defprotocol/2` for more information on how structs
132-
can be used with protocols for polymorphic dispatch. Also
133-
see `Kernel.struct/2` and `Kernel.struct!/2` for examples on
134-
how to create and update structs dynamically.
132+
%{user | age: 28}
135133
136134
## Pattern matching on struct names
137135

lib/elixir/pages/getting-started/structs.md

+2
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ iex> %User{} = %{}
6969
** (MatchError) no match of right hand side value: %{}
7070
```
7171

72+
For more details on creating, updating, and pattern matching structs, see the documentation for `%/2`.
73+
7274
## Structs are bare maps underneath
7375

7476
Structs are simply maps with a "special" field named `__struct__` that holds the name of the struct:

0 commit comments

Comments
 (0)