Is there a root Elixir module?

Clash Royale CLAN TAG#URR8PPP
Is there a root Elixir module?
When I define a module in Elixir and perform a to_string operation on it like this in IEX
to_string
MyModule |> to_string
MyModule |> to_string
I will get the result,
"Elixir.MyModule"
"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?
Elixir
This website says it is a namespace but I have read other articles that say Elixir does not support namespaces.
2 Answers
2
In "Programming Elixir 1.6" Dave Thomas gives this example:
defmodule Outer do
defmodule Inner do
def inner_func do
end
end
def outer_func do
Inner.inner_func
end
end
Outer.outer_func
Outer.Inner.inner_func
defmodule Outer do
defmodule Inner do
def inner_func do
end
end
def outer_func do
Inner.inner_func
end
end
Outer.outer_func
Outer.Inner.inner_func
He then explains:
"Module nesting in Elixir is an illusion—all modules are defined at the top level. When we define a module inside another, Elixir simply prepends the outer module name to the inner module name, putting a dot between the two. This means we can directly define a nested module."
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":
Elixir
MyModule
:"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:
Elixir
iex(6)> MyModule == Elixir.MyModule
true
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Sidenote: Elixir does surely support namespaces (well, if we are using the common meaning of “namespaces”.)
– mudasobwa
Aug 9 at 6:59