Elixirの対話モードでモジュールの動作を確認してみた

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

https://elixirschool.com/ja/lessons/basics/modules

# 対話モードで実行
以下のコマンドを実行しました。
```
$ 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 Example do
...(1)> def greeting(name) do
...(1)> "Hello #{name}."
...(1)> end
...(1)> end
{:module, Example,
 <<70, 79, 82, 49, 0, 0, 5, 208, 66, 69, 65, 77, 65, 116, 85, 56, 0, 0, 0, 186,
   0, 0, 0, 18, 14, 69, 108, 105, 120, 105, 114, 46, 69, 120, 97, 109, 112, 108,
   101, 8, 95, 95, 105, 110, 102, 111, 95, ...>>, {:greeting, 1}}
iex(2)> Example.greeting "Sean"
"Hello Sean."
iex(3)> defmodule Example.Greetings do
...(3)> def morning(name) do
...(3)> "Good morning #{name}."
...(3)> end
...(3)> def evening(name) do
...(3)> "Good night #{name}."
...(3)> end
...(3)> end
{:module, Example.Greetings,
 <<70, 79, 82, 49, 0, 0, 6, 228, 66, 69, 65, 77, 65, 116, 85, 56, 0, 0, 0, 203,
   0, 0, 0, 19, 24, 69, 108, 105, 120, 105, 114, 46, 69, 120, 97, 109, 112, 108,
   101, 46, 71, 114, 101, 101, 116, 105, 110, ...>>, {:evening, 1}}
iex(4)> Example.Greetings.morning "Sean"
"Good morning Sean."
iex(5)> defmodule Example do
...(5)> @greeting "Hello"
...(5)> def greeting(name) do
...(5)> ~s(#{@greeting} #{name}.)
...(5)> end
...(5)> end
warning: redefining module Example (current version defined in memory)
  iex:5

{:module, Example,
 <<70, 79, 82, 49, 0, 0, 5, 212, 66, 69, 65, 77, 65, 116, 85, 56, 0, 0, 0, 186,
   0, 0, 0, 18, 14, 69, 108, 105, 120, 105, 114, 46, 69, 120, 97, 109, 112, 108,
   101, 8, 95, 95, 105, 110, 102, 111, 95, ...>>, {:greeting, 1}}
iex(6)> defmodule Example.User do
...(6)> defstruct name: "Sean", roles: []
...(6)> end
{:module, Example.User,
 <<70, 79, 82, 49, 0, 0, 6, 236, 66, 69, 65, 77, 65, 116, 85, 56, 0, 0, 0, 201,
   0, 0, 0, 19, 19, 69, 108, 105, 120, 105, 114, 46, 69, 120, 97, 109, 112, 108,
   101, 46, 85, 115, 101, 114, 8, 95, 95, ...>>,
 %Example.User{name: "Sean", roles: []}}
iex(7)> %Example.User{}
%Example.User{name: "Sean", roles: []}
iex(8)> %Example.User{name: "Steve"}
%Example.User{name: "Steve", roles: []}
iex(9)> %Example.User{name: "Steve", roles: [:manager]}
%Example.User{name: "Steve", roles: [:manager]}
iex(10)> steve = %Example.User{name: "Steve"}
%Example.User{name: "Steve", roles: []}
iex(11)> sean = %{steve | name: "Sean"}
%Example.User{name: "Sean", roles: []}
iex(12)> %{name: "Sean"} = sean
%Example.User{name: "Sean", roles: []}
iex(13)> inspect(sean)
"%Example.User{name: \"Sean\", roles: []}"
iex(14)> defmodule Example.User do
...(14)> @derive {Inspect, only: [:name]}
...(14)> defstruct name: nil, roles: []
...(14)> end
warning: redefining module Example.User (current version defined in memory)
  iex:14

{:module, Example.User,
 <<70, 79, 82, 49, 0, 0, 6, 232, 66, 69, 65, 77, 65, 116, 85, 56, 0, 0, 0, 201,
   0, 0, 0, 19, 19, 69, 108, 105, 120, 105, 114, 46, 69, 120, 97, 109, 112, 108,
   101, 46, 85, 115, 101, 114, 8, 95, 95, ...>>, #Example.User<name: nil, ...>}
iex(15)> sean = %Example.User{name: "Sean"}
#Example.User<name: "Sean", ...>
iex(16)> inspect(sean)
"#Example.User<name: \"Sean\", ...>"
iex(17)> defmodule Sayings.Greetings do
...(17)> def basic(name), do: "Hi, #{name}"
...(17)> end
{:module, Sayings.Greetings,
 <<70, 79, 82, 49, 0, 0, 5, 180, 66, 69, 65, 77, 65, 116, 85, 56, 0, 0, 0, 193,
   0, 0, 0, 18, 24, 69, 108, 105, 120, 105, 114, 46, 83, 97, 121, 105, 110, 103,
   115, 46, 71, 114, 101, 101, 116, 105, 110, ...>>, {:basic, 1}}
iex(18)> defmodule Example do
...(18)> alias Sayings.Greetings
...(18)> def greeting(name), do: Greetings.basic(name)
...(18)> end
warning: redefining module Example (current version defined in memory)
  iex:18

{:module, Example,
 <<70, 79, 82, 49, 0, 0, 5, 28, 66, 69, 65, 77, 65, 116, 85, 56, 0, 0, 0, 173,
   0, 0, 0, 16, 14, 69, 108, 105, 120, 105, 114, 46, 69, 120, 97, 109, 112, 108,
   101, 8, 95, 95, 105, 110, 102, 111, 95, ...>>, {:greeting, 1}}
iex(19)> defmodule Example do
...(19)> def greeting(name), do: Sayings.Greetings.basic(name)
...(19)> end
warning: redefining module Example (current version defined in memory)
  iex:19

{:module, Example,
 <<70, 79, 82, 49, 0, 0, 5, 28, 66, 69, 65, 77, 65, 116, 85, 56, 0, 0, 0, 173,
   0, 0, 0, 16, 14, 69, 108, 105, 120, 105, 114, 46, 69, 120, 97, 109, 112, 108,
   101, 8, 95, 95, 105, 110, 102, 111, 95, ...>>, {:greeting, 1}}
iex(20)> defmodule Example do
...(20)> alias Sayings.Greetings, as: Hi
...(20)> def print_message(name), do: Hi.basic(name)
...(20)> end
warning: redefining module Example (current version defined in memory)
  iex:20

{:module, Example,
 <<70, 79, 82, 49, 0, 0, 5, 52, 66, 69, 65, 77, 65, 116, 85, 56, 0, 0, 0, 178,
   0, 0, 0, 16, 14, 69, 108, 105, 120, 105, 114, 46, 69, 120, 97, 109, 112, 108,
   101, 8, 95, 95, 105, 110, 102, 111, 95, ...>>, {:print_message, 1}}
iex(21)> defmodule Example do
...(21)> alias Sayings.{Greetings, Farewells}
...(21)> end
warning: redefining module Example (current version defined in memory)
  iex:21

warning: unused alias Farewells
  iex:22

warning: unused alias Greetings
  iex:22

{:module, Example,
 <<70, 79, 82, 49, 0, 0, 3, 204, 66, 69, 65, 77, 65, 116, 85, 56, 0, 0, 0, 133,
   0, 0, 0, 13, 14, 69, 108, 105, 120, 105, 114, 46, 69, 120, 97, 109, 112, 108,
   101, 8, 95, 95, 105, 110, 102, 111, 95, ...>>,
 [Sayings.Greetings, Sayings.Farewells]}
iex(22)> last([1, 2, 3])
** (CompileError) iex:22: undefined function last/1

iex(22)> import List
List
iex(23)> last([1, 2, 3])
3
iex(24)> import List, only: [last: 1]
List
iex(25)> first([1, 2, 3])
** (CompileError) iex:25: undefined function first/1

iex(25)> last([1, 2, 3])
3
iex(26)> import List, except: [last: 1]
List
iex(27)> first([1, 2, 3])
** (CompileError) iex:27: undefined function first/1

iex(27)> last([1, 2, 3])
** (CompileError) iex:27: undefined function last/1

iex(27)> import List, only: :functions
List
iex(28)> import List, only: :macros
List
iex(29)> defmodule Example do
...(29)> require SuperMacros
...(29)> SuperMacros.do_stuff
...(29)> end
warning: redefining module Example (current version defined in memory)
  iex:29

** (CompileError) iex:30: module SuperMacros is not loaded and could not be found

iex(29)> defmodule Hello do
...(29)> defmacro __using__(_opts) do
...(29)> quote do
...(29)> def hello(name), do: "Hi, #{name}"
...(29)> end
...(29)> end
...(29)> end
{:module, Hello,
 <<70, 79, 82, 49, 0, 0, 5, 216, 66, 69, 65, 77, 65, 116, 85, 56, 0, 0, 0, 147,
   0, 0, 0, 14, 12, 69, 108, 105, 120, 105, 114, 46, 72, 101, 108, 108, 111, 8,
   95, 95, 105, 110, 102, 111, 95, 95, 10, ...>>, {:__using__, 1}}
iex(30)> defmodule Example do
...(30)> use Hello
...(30)> end
warning: redefining module Example (current version defined in memory)
  iex:30

{:module, Example,
 <<70, 79, 82, 49, 0, 0, 5, 196, 66, 69, 65, 77, 65, 116, 85, 56, 0, 0, 0, 183,
   0, 0, 0, 18, 14, 69, 108, 105, 120, 105, 114, 46, 69, 120, 97, 109, 112, 108,
   101, 8, 95, 95, 105, 110, 102, 111, 95, ...>>, {:hello, 1}}
iex(31)> Example.hello("Sean")
"Hi, Sean"
iex(32)> defmodule Hello do
...(32)> defmacro __using__(opts) do
...(32)> greeting = Keyword.get(opts, :greeting, "Hi")
...(32)> quote do
...(32)> def hello(name), do: unquote(greeting) <> ", " <> name
...(32)> end
...(32)> end
...(32)> end
warning: redefining module Hello (current version defined in memory)
  iex:32

{:module, Hello,
 <<70, 79, 82, 49, 0, 0, 6, 84, 66, 69, 65, 77, 65, 116, 85, 56, 0, 0, 0, 185,
   0, 0, 0, 20, 12, 69, 108, 105, 120, 105, 114, 46, 72, 101, 108, 108, 111, 8,
   95, 95, 105, 110, 102, 111, 95, 95, 10, ...>>, {:__using__, 1}}
iex(33)> defmodule Example do
...(33)> use Hello, greeting: "Hola"
...(33)> end
warning: redefining module Example (current version defined in memory)
  iex:33

{:module, Example,
 <<70, 79, 82, 49, 0, 0, 5, 108, 66, 69, 65, 77, 65, 116, 85, 56, 0, 0, 0, 153,
   0, 0, 0, 16, 14, 69, 108, 105, 120, 105, 114, 46, 69, 120, 97, 109, 112, 108,
   101, 8, 95, 95, 105, 110, 102, 111, 95, ...>>, {:hello, 1}}
iex(34)> Example.hello("Sean")
"Hola, Sean"
iex(35)> 
```

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