简体   繁体   English

使用另一个模块中的函数在erlang中生成函数

[英]spawn function in erlang using function in another module

I have been learning erlang for the past week and am going through Joe Armstrong's Pragmatic erlang book . 过去一周我一直在学习erlang,并且正在阅读Joe Armstrong的实用的erlang书。 I was writing some code to spawn processes and have come across a situation I have a function in module myatom.erl which looks like this 我正在编写一些代码来生成进程,并且遇到了我在模块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. 

There is a function in another module named tloop.erl 另一个名为tloop.erl的模块中有一个函数

loop() ->
   receive 
        { From , No } -> From ! { self(), No*4};
        Other -> void
   end. 

if I am to use start() to spawn loop in the erlang shell , how can I do it ? 如果我要使用start()在erlang shell中生成循环,我该怎么办呢? I get the following error when I do 我这样做时会出现以下错误

anatom:start(atomname,tloop:loop). 

Thanks in advance ! 提前致谢 !

anatom:start(myatom,fun tloop:loop). 
* 2: syntax error before: ')

You must write the following 您必须写下以下内容

anatom:start(myatom, fun tloop:loop/0).

You have to specify the arity (number of arguments) of the function, as in erlang functions with the same name but different arity are not considered to be the same function. 您必须指定函数的arity(参数个数),因为在具有相同名称但不同arity的erlang函数中,不会将其视为相同的函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM