Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elixir: Module names with dot and nested modules - are they equivalent?

Tags:

elixir

Are below codes equivalent? As to invoke a module method, in both cases, one will use Utilities.StringUtils.some_method(...)

Nested modules

defmodule Utilities do
   defmodule StringUtils do
   end
end

Modules with dot in the name

defmodule Utilities.StringUtils do
end
like image 230
Wand Maker Avatar asked Aug 30 '15 11:08

Wand Maker


2 Answers

Yes and no. The first definition automatically defines an alias based on the module name:

defmodule Utilities do
  defmodule StringUtils do
  end

  # Can access the module as StringUtils
end

While the second:

defmodule Utilities.StringUtils do
  # Cannot access the module as StringUtils
end

Other than that, the code and module defined by both are exactly the same.

like image 78
José Valim Avatar answered Sep 18 '22 11:09

José Valim


Yes, both are translated exactly to the symbol (in Erlang a module is referenced by its symbol):

:"Elixir.Utilities.StringUtils"

There aren't really nested modules in Erlang, it's just something Elixir simulated.

like image 27
Ramon Snir Avatar answered Sep 18 '22 11:09

Ramon Snir