Reputation: 16771
Regarding the Phoenix Framework: I was just wondering what the difference is between writing
defmodule MyApp.User do
# some code
end
and just
defmodule User do
# some code
end
later on it would be easier to just write User.function than MyApp.User.function
Upvotes: 1
Views: 106
Reputation: 9109
It can be applied with the concept of nesting in elixir. Often you would like group a certain modules according to their functionality or just for the sake of naming convinence. Here is a straight example from the docs
defmodule Foo do
defmodule Bar do
end
end
Which is same as
defmodule Elixir.Foo do
defmodule Elixir.Foo.Bar do
end
alias Elixir.Foo.Bar, as: Bar
end
It is important to note that in Elixir,there is no need to define the Outer module to able to use Outer.Inner
module name, as the language translates all module names to atoms anyway. You can define arbitrarily-nested modules without defining any module in the chain (e.g., Foo.Bar.Baz without defining Foo or Foo.Bar first).
A solid example is available at the elixir docs.
Upvotes: 1
Reputation: 84180
This is namespacing you module to avoid clashes. Imagine a scenario where you call your module User
and then user a library called user which also defines a User
module. You would have a collision.
You can alias in modules that use your User
module though:
defmodule MyApp.SomeModule do
alias MyApp.User
def foo do
User.some_function
end
end
Upvotes: 6