[英]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.