关于Erlang函数,尤其是函数的标识符

rua*_*hao 3 erlang

我有关于Erlang功能的问题.查看Erlang shell中的代码:

1> F1 = fun() -> timer:sleep(1000) end.
#Fun<erl_eval.20.111823515>
2> F2 = fun() -> io:format("hello world~n", []) end.
#Fun<erl_eval.20.111823515>
Run Code Online (Sandbox Code Playgroud)

F1F2是不同的,但为什么他们都有标识符#Fun<erl_eval.20.111823515>?这些神奇数字意味着什么?


ERTS手册中有一段说:

When interpreting the data for a process, it is helpful to know that anonymous
function objects (funs) are given a name constructed from the name of the
function in which they are created, and a number (starting with 0) indicating
the number of that fun within that function.
Run Code Online (Sandbox Code Playgroud)

我也抓不住这一段的含义,请你解释一下吗?

rvi*_*ing 6

不要在匿名函数的名称中读出太多含义.所有你可以安全地摆脱它的是创建它们的模块的名称.您可以尝试计算模块中的乐趣,找到哪一个,但我不打扰.

话虽这么说,但两个乐趣都有同名的原因.在shell中输入的表达式不会被编译,而是由模块中的解释器进行评估erl_eval.这个模块有一个解释每个arity乐趣的乐趣.因此,erl_eval对于arity 1的乐趣来说,有一个乐趣#Fun<erl_eval.20.111823515>.哈基,但它的工作原理.