When writing my code, I always find __MODULE__ very useful to use as alias of that module “inner dependencies”, ex:
alias __MODULE__.{Impl, Helper, ...}
But one thing that I always find missing is that when I want to get some dependency for a module in the same “level” that my current module, I need to write the full module path, ex:
defmodule My.Big.Module.Path.Something do
alias My.Big.Module.Path.SomethingElse
...
end
I feel that having something like __PARENT_MODULE__ would be useful in these cases. Also, the impact on the existing eco-system would be zero since it doesn’t break any existing code.
Here are more “concrete examples” of when this can come in handy:
When organizing behaviours
The way I organize them is something like this:
# The behaviour module
Some.Module.MyBehaviour
# The modules implementing it
Some.Module.MyBehaviour.Impl1
Some.Module.MyBehaviour.Impl2
Some.Module.MyBehaviour.Impl3
In these cases, I could write something like @behaviour __PARENT_MODULE__ and @impl __PARENT_MODULE__ instead of @behaviour Some.Module.MyBehaviour and @impl Some.Module.MyBehaviour
When organizing gen servers and scoped business logic
If my genservers are getting big or complex, I like to split them into smaller parts, normally what I do is have something like this:
# This module will have the genserver child_spec and public APIS
Some.Module.Path.MyGenServer
# This module will have all the genserver code implementation (handle_call, start_async, etc)
Some.Module.Path.MyGenServer.Server
# This module will have all the genserver business logic
Some.Module.Path.MyGenServer.Impl
# And this module will have the genserver state struct and how to create/update it
Some.Module.Path.MyGenServer.State
Now, for example, the Server module needs to call Impl functions, and the Impl module needs to call State functions. Making the usage of __PARENT_MODULE__ useful here.
I don’t know if that is normally an issue for other people, but the project I work is pretty big and have some pretty big module names, so having something like that would make writing some code way faster for me.






















