Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a root Elixir module?

Tags:

elixir

When I define a module in Elixir and perform a to_string operation on it like this in IEX

MyModule |> to_string

I will get the result,

"Elixir.MyModule"

Is there a root level Elixir module that all other modules live in? Why is there an Elixir prefix that looks like another module?

This website says it is a namespace but I have read other articles that say Elixir does not support namespaces.

like image 967
Audus Avatar asked Dec 03 '25 09:12

Audus


1 Answers

I believe this is for make a difference between Elixir and Erlang modules. All elixir modules are starting with Elixir prefix. MyModule is just an alias for an atom :"Elixir.MyModule":

iex(1)> defmodule MyModule, do: def f(), do: "MyModule.f()"

Standard function call:

iex(2)> MyModule.f()
"MyModule.f()"

Erlang-style function call:

iex(3)> :"Elixir.MyModule".f()
"MyModule.f()"

Is the atom equal to alias?

iex(4)> :"Elixir.MyModule" == MyModule
true
iex(5)> :"Elixir.MyModule" == Elixir.MyModule
true

Elixir prefix is for convenience:

iex(6)> MyModule == Elixir.MyModule
true
like image 110
Grych Avatar answered Dec 05 '25 10:12

Grych



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!