我在Erlang的文档中发现函数spawn具有这样的格式spawn(Module, Name, Args) -> pid().我试过了.它不起作用.什么地方出了错?
代码:
-module(tut).
-export([main/0]).
main() ->
spawner(),
spawner(),
spawner().
for(Counter) when Counter == 0 ->
io:fwrite("0");
for(Counter) when Counter > 0 ->
io:fwrite("~p\n", [Counter]),
for(Counter -1).
spawner() ->
spawn(tut, for, [50]).
Run Code Online (Sandbox Code Playgroud)
控制台输出:
68> c(tut).
tut.erl:12: Warning: function for/1 is unused
{ok,tut}
69> tut:main().
<0.294.0>
=ERROR REPORT==== 6-Sep-2017::15:06:29 ===
Error in process <0.292.0> with exit value:
{undef,[{tut,for,"2",[]}]}
70>
=ERROR REPORT==== 6-Sep-2017::15:06:29 ===
Error in process <0.293.0> with exit value:
{undef,[{tut,for,"2",[]}]}
=ERROR REPORT==== 6-Sep-2017::15:06:29 ===
Error in process <0.294.0> with exit value:
{undef,[{tut,for,"2",[]}]}
Run Code Online (Sandbox Code Playgroud)
spawn只有当您调用的函数被导出时,三参数版本才有效.为了使这个工作起作用,您可以导出该for函数main,或者使用单参数版本spawn,传递一个发出本地调用的匿名函数('fun'),从而绕过导出函数的需要:
spawn(fun() -> for(50) end)
Run Code Online (Sandbox Code Playgroud)