Elixirの対話モードで構造体の動作を確認してみた

# 概要
Elixirの対話モードで構造体の動作を確認してみました。以下のページを参考にしました。

https://dev.to/gumi/elixir-15--4f43

# 対話モードで実行
以下のコマンドを実行しました。
```
$ iex
Erlang/OTP 24 [erts-12.2.1] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:1] [jit]

Interactive Elixir (1.12.2) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> defmodule User do
...(1)> defstruct name: "John", age: 27
...(1)> end
{:module, User,
 <<70, 79, 82, 49, 0, 0, 6, 196, 66, 69, 65, 77, 65, 116, 85, 56, 0, 0, 0, 193,
   0, 0, 0, 19, 11, 69, 108, 105, 120, 105, 114, 46, 85, 115, 101, 114, 8, 95,
   95, 105, 110, 102, 111, 95, 95, 10, 97, ...>>, %User{age: 27, name: "John"}}
iex(2)> %User{}
%User{age: 27, name: "John"}
iex(3)> %User{name: "Meg"}
%User{age: 27, name: "Meg"}
iex(4)> %User{age: 20}
%User{age: 20, name: "John"}
iex(5)> %User{name: 20}
%User{age: 27, name: 20}
iex(6)> %User{oops: :field}
** (KeyError) key :oops not found
    expanding struct: User.__struct__/1
    iex:6: (file)
iex(6)> john = %User{}
%User{age: 27, name: "John"}
iex(7)> john.name
"John"
iex(8)> meg = %{john | name: "Meg"}
%User{age: 27, name: "Meg"}
iex(9)> %{meg | oops: :field}
** (KeyError) key :oops not found in: %User{age: 27, name: "Meg"}
    (stdlib 3.17) :maps.update(:oops, :field, %User{age: 27, name: "Meg"})
    (stdlib 3.17) erl_eval.erl:256: anonymous fn/2 in :erl_eval.expr/5
    (stdlib 3.17) lists.erl:1267: :lists.foldl/3
iex(9)> meg = %{john | name: "Meg"}
%User{age: 27, name: "Meg"}
iex(10)> %User{name: name} = meg
%User{age: 27, name: "Meg"}
iex(11)> name
"Meg"
iex(12)> %User{} = %{age: 27, name: "John"}
** (MatchError) no match of right hand side value: %{age: 27, name: "John"}

iex(12)> is_map(john)
true
iex(13)> john.__struct__
User
iex(14)> john_map = %{age: 27, name: "John"}
%{age: 27, name: "John"}
iex(15)> john_struct = %User{age: 27, name: "John"}
%User{age: 27, name: "John"}
iex(16)> john_map[:name]
"John"
iex(17)> john_struct[:name]
** (UndefinedFunctionError) function User.fetch/2 is undefined (User does not implement the Access behaviour)
    User.fetch(%User{age: 27, name: "John"}, :name)
    (elixir 1.12.2) lib/access.ex:285: Access.get/3
iex(17)> john_map.name
"John"
iex(18)> john_struct.name
"John"
iex(19)> Enum.each(john_map, fn({field, value}) -> IO.puts(value) end)
warning: variable "field" is unused (if the variable is not meant to be used, prefix it with an underscore)
  iex:19

27
John
:ok
iex(20)> Enum.each(john_struct, fn({field, value}) -> IO.puts(value) end)
warning: variable "field" is unused (if the variable is not meant to be used, prefix it with an underscore)
  iex:20

** (Protocol.UndefinedError) protocol Enumerable not implemented for %User{age: 27, name: "John"} of type User (a struct)
    (elixir 1.12.2) lib/enum.ex:1: Enumerable.impl_for!/1
    (elixir 1.12.2) lib/enum.ex:141: Enumerable.reduce/3
    (elixir 1.12.2) lib/enum.ex:3952: Enum.each/2
iex(20)> kurt = Map.put(%User{}, :name, "Kurt")
%User{age: 27, name: "Kurt"}
iex(21)> takashi = Map.merge(kurt, %User{name: "Takashi"})
%User{age: 27, name: "Takashi"}
iex(22)> Map.keys(takashi)
[:__struct__, :age, :name]
iex(23)> defmodule User do
...(23)> defstruct [:name, :age]
...(23)> end
warning: redefining module User (current version defined in memory)
  iex:23

{:module, User,
 <<70, 79, 82, 49, 0, 0, 6, 192, 66, 69, 65, 77, 65, 116, 85, 56, 0, 0, 0, 193,
   0, 0, 0, 19, 11, 69, 108, 105, 120, 105, 114, 46, 85, 115, 101, 114, 8, 95,
   95, 105, 110, 102, 111, 95, 95, 10, 97, ...>>, %User{age: nil, name: nil}}
iex(24)> %User{name: "John"}
%User{age: nil, name: "John"}
iex(25)> defmodule User do
...(25)> @enforce_keys [:name]
...(25)> defstruct [:name, :age]
...(25)> end
warning: redefining module User (current version defined in memory)
  iex:25

{:module, User,
 <<70, 79, 82, 49, 0, 0, 9, 128, 66, 69, 65, 77, 65, 116, 85, 56, 0, 0, 1, 29,
   0, 0, 0, 28, 11, 69, 108, 105, 120, 105, 114, 46, 85, 115, 101, 114, 8, 95,
   95, 105, 110, 102, 111, 95, 95, 10, 97, ...>>, %User{age: nil, name: nil}}
iex(26)> %User{name: "John"}
%User{age: nil, name: "John"}
iex(27)> %User{age: 27}
** (ArgumentError) the following keys must also be given when building struct User: [:name]
    expanding struct: User.__struct__/1
    iex:27: (file)
iex(27)> 
```

# まとめ
何かの役に立てばと。