[英]spawn function in erlang using function in another module
过去一周我一直在学习erlang,并且正在阅读Joe Armstrong的实用的erlang书。 我正在编写一些代码来生成进程,并且遇到了我在模块myatom.erl中有一个函数的情况,看起来像这样
start(anatom,Fun) ->
case whereis(anatom) of
undefined ->
Pid = spawn(Fun),
try register(anatom,Pid) of
true -> true
catch
error:Reason ->
Reason
end;
Other -> {error,already_defined}
end.
另一个名为tloop.erl的模块中有一个函数
loop() ->
receive
{ From , No } -> From ! { self(), No*4};
Other -> void
end.
如果我要使用start()在erlang shell中生成循环,我该怎么办呢? 我这样做时会出现以下错误
anatom:start(atomname,tloop:loop).
提前致谢 !
anatom:start(myatom,fun tloop:loop).
* 2: syntax error before: ')
您必须写下以下内容
anatom:start(myatom, fun tloop:loop/0).
您必须指定函数的arity(参数个数),因为在具有相同名称但不同arity的erlang函数中,不会将其视为相同的函数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.