@@ -35,9 +35,9 @@ We can now create "instances" of this struct by using the `%User{}` syntax:
35
35
36
36
``` iex
37
37
iex> %User{}
38
- %User{name: "john", age: 27 }
38
+ %User{age: 27, name: "john" }
39
39
iex> %User{name: "meg"}
40
- %User{name: "meg", age: 27 }
40
+ %User{age: 27, name: "meg" }
41
41
iex> is_map(%User{})
42
42
true
43
43
```
@@ -53,11 +53,11 @@ When discussing maps, we demonstrated how we can access and update existing fiel
53
53
54
54
``` iex
55
55
iex> john = %User{}
56
- %User{name: "john", age: 27 }
56
+ %User{age: 27, name: "john" }
57
57
iex> john.name
58
58
"john"
59
59
iex> meg = %{john | name: "meg"}
60
- %User{name: "meg", age: 27 }
60
+ %User{age: 27, name: "meg" }
61
61
iex> %{meg | oops: :field}
62
62
** (ArgumentError) argument error
63
63
```
@@ -68,7 +68,7 @@ Structs can also be used in pattern matching and they guarantee the structs are
68
68
69
69
``` iex
70
70
iex> %User{name: name} = john
71
- %User{name: "john", age: 27 }
71
+ %User{age: 27, name: "john" }
72
72
iex> name
73
73
"john"
74
74
iex> %User{} = %{}
@@ -86,7 +86,7 @@ Overall, a struct is just a bare map with default fields. Notice we say it is a
86
86
87
87
``` iex
88
88
iex> user = %User{}
89
- %User{name: "john", age: 27 }
89
+ %User{age: 27, name: "john" }
90
90
iex> user[:name]
91
91
** (Protocol.UndefinedError) protocol Access not implemented for %User{age: 27, name: "john"}
92
92
```
@@ -98,4 +98,13 @@ iex> Dict.get(%User{}, :name)
98
98
** (ArgumentError) unsupported dict: %User{name: "john", age: 27}
99
99
```
100
100
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
+
101
110
We will cover how structs interacts with protocols in the next chapter.
0 commit comments