Search code examples
erlangelixir

Is there an equivalent to __MODULE__ for named functions in Elixir/ Erlang?


Is there an equivalent for retrieving the name of a function just like like __MODULE__ retrieves the name of a Module in Elixir/Erlang?

Example:

defmodule Demo do
  def home_menu do
    module_name = __MODULE__
    func_name = :home_menu 
    # is there a __FUNCTION__
  end
End

EDITED

The selected answer works,

but calling the returned function name with apply/3 yields this error:

[error] %UndefinedFunctionError{arity: 4, exports: nil, function: :public_home, module: Demo, reason: nil}

I have a function :

defp public_home(u, m, msg, reset) do

end

The function in question will strictly be called within its module.

Is there a way to dynamically call a private function by name within its own module?


Solution

  • ▶ defmodule M, do: def m, do: __ENV__.function  
    ▶ M.m                                                 
    #⇒ {:m, 0}
    

    Essentially, __ENV__ structure contains everything you might need.