LoginSignup
3
0

Elixirの対話モードでaliasとrequireおよびimportの動作を確認してみた

Posted at

概要

Elixirの対話モードでaliasとrequireおよびimportの動作を確認してみました。以下のページを参考にしました。

対話モードで実行

以下のコマンドを実行しました。

$ 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 Sayings.Greetings do
...(1)> def basic(name), do: "hello, #{name}"
...(1)> end
{:module, Sayings.Greetings,
 <<70, 79, 82, 49, 0, 0, 5, 184, 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(2)> defmodule Example do
...(2)> def greeting(name), do: Sayings.Greetings.basic(name)
...(2)> end
{: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(3)> Example.greeting("world")
"hello, world"
iex(4)> defmodule Example do
...(4)> alias Sayings.Greetings
...(4)> def greeting(name), do: Greetings.basic(name)
...(4)> end
warning: redefining module Example (current version defined in memory)
  iex:4

{: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(5)> alias Example, as: String
Example
iex(6)> String.greeting("tokyo")
"hello, tokyo"
iex(7)> Elixir.String.length("hello")
5
iex(8)> defmodule Example do
...(8)> alias Sayings.Greetings, as: Hi
...(8)> def greeting(name), do: Hi.basic(name)
...(8)> def greeting_ex(name), do: Hi.basic(name) <> "!!"
...(8)> end
warning: redefining module Example (current version defined in memory)
  iex:8

{:module, Example,
 <<70, 79, 82, 49, 0, 0, 6, 32, 66, 69, 65, 77, 65, 116, 85, 56, 0, 0, 0, 185,
   0, 0, 0, 17, 14, 69, 108, 105, 120, 105, 114, 46, 69, 120, 97, 109, 112, 108,
   101, 8, 95, 95, 105, 110, 102, 111, 95, ...>>, {:greeting_ex, 1}}
iex(9)> Example.greeting_ex("world")     
"hello, world!!"
iex(10)> defmodule Example do
...(10)> def greeting(name) do
...(10)> alias Sayings.Greetings, as: Hi
...(10)> Hi.basic(name)
...(10)> end
...(10)> def greeting_ex(name), do: Hi.basic(name) <> "!!"
...(10)> end
warning: redefining module Example (current version defined in memory)
  iex:10

{:module, Example,
 <<70, 79, 82, 49, 0, 0, 6, 80, 66, 69, 65, 77, 65, 116, 85, 56, 0, 0, 0, 195,
   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_ex, 1}}
iex(11)> Example.greeting("world")
"hello, world"
iex(12)> Example.greeting_ex("world")
** (UndefinedFunctionError) function Hi.basic/1 is undefined (module Hi is not available)
    Hi.basic("world")
    iex:15: Example.greeting_ex/1
iex(12)> Integer.is_odd(5)
** (UndefinedFunctionError) function Integer.is_odd/1 is undefined or private. However there is a macro with the same name and arity. Be sure to require Integer if you intend to invoke this macro
    (elixir 1.12.2) Integer.is_odd(5)
iex(12)> require Integer
Integer
iex(13)> Integer.is_odd(5)
true
iex(14)> import List
List
iex(15)> first([1, 2, 3])
1
iex(16)> last([1, 2, 3])
3
iex(17)> flatten([1, [[2], 3]])
[1, 2, 3]
iex(18)> import List, only: [first: 1, last: 1]
List
iex(19)> first([1, 2, 3])
1
iex(20)> flatten([1, [[2], 3]])
** (CompileError) iex:20: undefined function flatten/1

iex(20)> import Integer, only: :macros
Integer
iex(21)> is_even(4)
true
iex(22)> digits(123)
** (CompileError) iex:22: undefined function digits/1

iex(22)> Integer.digits(123)
[1, 2, 3]
iex(23)> import Integer, only: :functions
Integer
iex(24)> digits(123)
[1, 2, 3]
iex(25)> is_odd(3)
** (CompileError) iex:25: undefined function is_odd/1

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

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

iex(26)> defmodule Example do
...(26)> def split(number) do
...(26)> import Integer, only: [digits: 1]
...(26)> digits(number)
...(26)> end
...(26)> def test(name), do: digits(name)
...(26)> end
warning: redefining module Example (current version defined in memory)
  iex:26

{:module, Example,
 <<70, 79, 82, 49, 0, 0, 5, 156, 66, 69, 65, 77, 65, 116, 85, 56, 0, 0, 0, 166,
   0, 0, 0, 17, 14, 69, 108, 105, 120, 105, 114, 46, 69, 120, 97, 109, 112, 108,
   101, 8, 95, 95, 105, 110, 102, 111, 95, ...>>, {:test, 1}}
iex(27)> defmodule Hello do
...(27)> defmacro __using__(_opts) do
...(27)> quote do
...(27)> def greeting(name), do: "hello, #{name}"
...(27)> end
...(27)> end
...(27)> end
{:module, Hello,
 <<70, 79, 82, 49, 0, 0, 5, 224, 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(28)> defmodule Example do
...(28)> use Hello
...(28)> end
warning: redefining module Example (current version defined in memory)
  iex:28

{:module, Example,
 <<70, 79, 82, 49, 0, 0, 5, 216, 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(29)> Example.greeting("world")
"hello, world"
iex(30)> defmodule Example do
...(30)> require Hello
...(30)> Hello.__using__(greeting: :value)
...(30)> end
warning: redefining module Example (current version defined in memory)
  iex:30

{:module, Example,
 <<70, 79, 82, 49, 0, 0, 5, 216, 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(31)> defmodule Hello do
...(31)> defmacro __using__(opts) do
...(31)> hello = Keyword.get(opts, :hello, "hello")
...(31)> quote do
...(31)> def greeting(name), do: unquote(hello) <> "," <> name
...(31)> end
...(31)> end
...(31)> end
warning: redefining module Hello (current version defined in memory)
  iex:31

{:module, Hello,
 <<70, 79, 82, 49, 0, 0, 6, 92, 66, 69, 65, 77, 65, 116, 85, 56, 0, 0, 0, 182,
   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(32)> defmodule Example do
...(32)> use Hello, hello: "こんにちは"
...(32)> end
warning: redefining module Example (current version defined in memory)
  iex:32

{:module, Example,
 <<70, 79, 82, 49, 0, 0, 5, 148, 66, 69, 65, 77, 65, 116, 85, 56, 0, 0, 0, 156,
   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(33)> Example.greeting("日本")
"こんにちは,日本"
iex(34)> is_atom(String)
true
iex(35)> to_string(String)
** (CompileError) iex:35: function to_string/1 imported from both Integer and Kernel, call is ambiguous
    (elixir 1.12.2) src/elixir_dispatch.erl:128: :elixir_dispatch.expand_import/6
    (elixir 1.12.2) src/elixir_dispatch.erl:91: :elixir_dispatch.dispatch_import/5
iex(35)> String == :"Elixir.String"
false
iex(36)> :lists.flatten([1, [[2], 3]])
[1, 2, 3]
iex(37)> defmodule Example do
...(37)> def greeting(name), do: "hello, #{name}"
...(37)> defmodule Greetings do
...(37)> def morning(name), do: "good morning, #{name}"
...(37)> end
...(37)> end
warning: redefining module Example (current version defined in memory)
  iex:37

{:module, Example,
 <<70, 79, 82, 49, 0, 0, 5, 188, 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, ...>>,
 {:module, Example.Greetings,
  <<70, 79, 82, 49, 0, 0, 5, 204, 66, 69, 65, 77, 65, 116, 85, 56, 0, 0, 0, 195,
    0, 0, 0, 18, 24, 69, 108, 105, 120, 105, 114, 46, 69, 120, 97, 109, 112,
    108, 101, 46, 71, 114, 101, ...>>, {:morning, 1}}}
iex(38)> Example.greeting("world")
"hello, world"
iex(39)> Example.Greetings.morning("tokyo")
"good morning, tokyo"
iex(40)> defmodule Example do
...(40)> def greeting(name), do: "hello, #{name}"
...(40)> defmodule Greetings do
...(40)> def morning(name), do: "good morning, #{name}"
...(40)> end
...(40)> def call_child(name), do: Greetings.morning(name)
...(40)> end
warning: redefining module Example (current version defined in memory)
  iex:40

warning: redefining module Example.Greetings (current version defined in memory)
  iex:42

{:module, Example,
 <<70, 79, 82, 49, 0, 0, 6, 184, 66, 69, 65, 77, 65, 116, 85, 56, 0, 0, 0, 230,
   0, 0, 0, 21, 14, 69, 108, 105, 120, 105, 114, 46, 69, 120, 97, 109, 112, 108,
   101, 8, 95, 95, 105, 110, 102, 111, 95, ...>>, {:call_child, 1}}
iex(41)> Example.call_child("japan")
"good morning, japan"
iex(42)> defmodule Example do
...(42)> def greeting(name), do: "hello, #{name}"
...(42)> end
warning: redefining module Example (current version defined in memory)
  iex:42

{:module, Example,
 <<70, 79, 82, 49, 0, 0, 5, 184, 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(43)> defmodule Example.Greetings do
...(43)> def morning(name), do: "good morning, #{name}"
...(43)> end
warning: redefining module Example.Greetings (current version defined in memory)
  iex:43

{:module, Example.Greetings,
 <<70, 79, 82, 49, 0, 0, 5, 204, 66, 69, 65, 77, 65, 116, 85, 56, 0, 0, 0, 195,
   0, 0, 0, 18, 24, 69, 108, 105, 120, 105, 114, 46, 69, 120, 97, 109, 112, 108,
   101, 46, 71, 114, 101, 101, 116, 105, 110, ...>>, {:morning, 1}}
iex(44)> defmodule Example.Greetings.Japan do
...(44)> def greeting(name), do: "こんにちは, #{name}"
...(44)> end
{:module, Example.Greetings.Japan,
 <<70, 79, 82, 49, 0, 0, 5, 248, 66, 69, 65, 77, 65, 116, 85, 56, 0, 0, 0, 202,
   0, 0, 0, 18, 30, 69, 108, 105, 120, 105, 114, 46, 69, 120, 97, 109, 112, 108,
   101, 46, 71, 114, 101, 101, 116, 105, 110, ...>>, {:greeting, 1}}
iex(45)> Example.Greetings.Japan.greeting("日本")
"こんにちは, 日本"
iex(46)> alias Example.Greetings.{US, Japan}
[Example.Greetings.US, Example.Greetings.Japan]
iex(47)> 

まとめ

何かの役に立てばと。

3
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
0