What are the scoping rule for Elixir alias?

Please give example but why obfuscate when it won’t be hidden anyway?

Module can even not exists at the compile time:

defmodule Foo do
  def setup(funcs) do
    Code.compile_quoted(quote bind_quoted: [funcs: funcs] do
      defmodule RuntimeLookup do
        for {name, value} <- funcs, do: def unquote(name)(), do: unquote(value)
      end
    end)
  end

  def main do
    setup(foo: 42)

    IO.inspect RuntimeLookup.foo()
  end
end

This pattern was used in OTP <21.2 when there was no persistent_term functionality, and nowadays it is still used from time to time when you need high performance calls, ex. OpenCensus can be configured to use such construct for speeding up metrics dispatch.

1 Like

For example, Protocol’s in Elixir make some hidden modules. My ProtocolEx makes a hidden description module, etc… You can still access them if you know the very ‘unique’ name they use (with $'s and so forth), just won’t be accidental.

2 Likes