Skip to content

Commit 67e9448

Browse files
committed
Clarify that structs can be used with Map module
Previously, the documentation only mentioned that the `Dict` module would not work with structs. Also clean up response values, since keys are output in alphabetical order.
1 parent da08b07 commit 67e9448

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

getting_started/15.markdown

+15-6
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ We can now create "instances" of this struct by using the `%User{}` syntax:
3535

3636
```iex
3737
iex> %User{}
38-
%User{name: "john", age: 27}
38+
%User{age: 27, name: "john"}
3939
iex> %User{name: "meg"}
40-
%User{name: "meg", age: 27}
40+
%User{age: 27, name: "meg"}
4141
iex> is_map(%User{})
4242
true
4343
```
@@ -53,11 +53,11 @@ When discussing maps, we demonstrated how we can access and update existing fiel
5353

5454
```iex
5555
iex> john = %User{}
56-
%User{name: "john", age: 27}
56+
%User{age: 27, name: "john"}
5757
iex> john.name
5858
"john"
5959
iex> meg = %{john | name: "meg"}
60-
%User{name: "meg", age: 27}
60+
%User{age: 27, name: "meg"}
6161
iex> %{meg | oops: :field}
6262
** (ArgumentError) argument error
6363
```
@@ -68,7 +68,7 @@ Structs can also be used in pattern matching and they guarantee the structs are
6868

6969
```iex
7070
iex> %User{name: name} = john
71-
%User{name: "john", age: 27}
71+
%User{age: 27, name: "john"}
7272
iex> name
7373
"john"
7474
iex> %User{} = %{}
@@ -86,7 +86,7 @@ Overall, a struct is just a bare map with default fields. Notice we say it is a
8686

8787
```iex
8888
iex> user = %User{}
89-
%User{name: "john", age: 27}
89+
%User{age: 27, name: "john"}
9090
iex> user[:name]
9191
** (Protocol.UndefinedError) protocol Access not implemented for %User{age: 27, name: "john"}
9292
```
@@ -98,4 +98,13 @@ iex> Dict.get(%User{}, :name)
9898
** (ArgumentError) unsupported dict: %User{name: "john", age: 27}
9999
```
100100

101+
Since structs are just maps, they will work with the `Map` module:
102+
103+
```iex
104+
iex> Map.put(%User{}, :name, "kurt")
105+
%User{age: 27, name: "kurt"}
106+
iex> Map.merge(%User{age: 27}, %User{name: "takashi"})
107+
%User{age: 27, name: "takashi"}
108+
```
109+
101110
We will cover how structs interacts with protocols in the next chapter.

0 commit comments

Comments
 (0)