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

Viewed 3785

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?

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

Essentially, __ENV__ structure contains everything you might need.

Yes, there is. In Erlang there are several predefined macros that should be able to provide the information you need:

% The name of the current function
?FUNCTION_NAME

% The arity of the current function (remember name alone isn't enough to identify a function in Erlang/Elixir)
?FUNCTION_ARITY 

% The file name of the current module
?FILE 

% The line number of the current line
?LINE 

Source: http://erlang.org/doc/reference_manual/macros.html#id85926

Related