Nested modules with the same name

defmodule Foo do
  def func1 do
  end
  def func2 do
    func1
  end
end
Foo.func2

The above works.

defmodule Foo do
  def func1 do
  end
  def func2 do
    Foo.func1
  end
end
Foo.func2

Works too.

But if add an inner module with same name like:

defmodule Foo do
  defmodule Foo do
  end
  def func1 do
  end
  def func2 do
    Foo.func1
  end
end
Foo.func2

Does not work.

Is there reason to allow a code like the second example?
It seems strange in a module that module’s name means that module .
I think it’s risk of unexpected behavior.

It is because when you defmodule a module inside of another module then it auto-aliases it as well. You only have this issue if the parent and child module names precisely match.

I’m of the opinion that no module should have the same name anywhere anyway, sub or not. :slight_smile: