0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Elixir Module | Namespace and Nesting #elixir

Posted at

Elixir Module | Namespace and Nesting

概要

Elixir の Module が持つ Namespace の役割と Nesting について。

Namespace

Elixir のモジュールは Namespace の役割を果たします。
モジュール外からモジュールの関数を利用する場合は SomeModuleName.function_name のように記述します。
モジュール内からモジュールの関数を利用する場合はモジュール名を省略できます。

defmodule SomeModule do
  def hoge do
    "hoge" <> hige
  end

  def hige do
    "hige"
  end
end

IO.puts SomeModule.hoge
  • 出力
hogehige

Nesting

モジュールはネストすることができます。
ネストしたモジュールを書く場合、 defmodule をネストさせる方法と、
ドットでつないで一気に記述方法があります。

defmodule ParentA do
  defmodule ChildA do
    def hoge do
      "hoge1"
    end
  end

  defmodule ChildB do
    def hoge do
      "hoge2"
    end
  end
end

IO.puts ParentA.ChildA.hoge
IO.puts ParentA.ChildB.hoge

defmodule ParentB.ChildA do
  def hoge do
    "hoge1"
  end
end

defmodule ParentB.ChildB do
  def hoge do
    "hoge2"
  end
end

IO.puts ParentB.ChildA.hoge
IO.puts ParentB.ChildB.hoge
  • 出力
hoge1
hoge2
hoge1
hoge2

関連

0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?