When you define an anonymous function in elixir you get a result like this.
#Function<6.90072148/1 in :erl_eval.expr/5>
What I've noticed is that the number is based on the arity of the function. So a 1 arg function is always
#Function<6.90072148/1 in :erl_eval.expr/5>
A two arg function is always
#Function<12.90072148/2 in :erl_eval.expr/5>
A three arg function is always
#Function<18.90072148/3 in :erl_eval.expr/5>
What is the number that's being returned, and how is it being derived?
The number consists of the index and a unique name of the function, which are generated by the compiler. Take a look at the implementation of the inspect protocol for functions. It contains this passage:
where
fun_info
refers to the result of a previous call to:erlang.fun_info
. This loosely translates to the following pseudo-code, where all interpolated values refer to elements offun_info
:As you have correctly observed, the part after the
/
indicates the arity.module
andname
show you where the function was defined.new_index
is a pointer into the module's function table, anduniq
is a hash value of the module generated by the compiler. When you invoke:erlang.fun_info
for a function, you will be able to recognize the values from the inspect string:The uniq value and the index taken together provide a way to uniquely identify an anonymous function. Be aware that inside
iex
these supposedly unique values will be very similar for all functions you create, but when the code is "properly" compiled, they will be unique. Consider the following iex session:Now compare that to running the following file with
elixir fun.exs
: