让我们考虑两种可能的情况
引用模块函数的函数
Fun = fun file:getcwd/0,
erlang:spawn(Node, Fun).
在这种情况下Fun
确实应该可以在远程端加载。
匿名函数
Fun = fun() -> io:format("My node is ~p~n", [node()]) end,
erlang:spawn(Node, Fun).
他们是也可调用.
总结
虽然功能隐含了一些限制,可以远程扩展,但这种形式仍然合适并且可以放弃,因为那样第二种情况就变得不可能了
Also
一些误解可能由此而来article
事实上,如果你跑erlang:fun_info
对于匿名函数,你会看到,它以 AST 的形式提供实现
(b@lol4t0-home)21> rp(erlang:fun_info(fun() -> io:format("My node is ~p~n", [node()]) end)).
[{pid,<0.96.0>},
{module,erl_eval},
{new_index,20},
{new_uniq,<<99,62,121,82,122,95,246,237,63,72,118,40,4,
25,16,50>>},
{index,20},
{uniq,52032458},
{name,'-expr/5-fun-3-'},
{arity,0},
{env,[{[],
{eval,#Fun<shell.21.31625193>},
{value,#Fun<shell.5.31625193>},
[{clause,1,[],[],
[{call,1,
{remote,1,{atom,1,io},{atom,1,format}},
[{string,1,"My node is ~p~n"},
{cons,1,{call,1,{atom,1,node},[]},{nil,1}}]}]}]}]},
{type,local}]