我在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]).
控制台输出:
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",[]}]}
答案 0 :(得分:5)
spawn
的三参数版本仅在您调用的函数导出时有效。要使其工作,除了for
之外,您还可以导出main
函数,或使用spawn
的单参数版本,传递匿名函数('有趣')进行本地调用,从而绕过了导出函数的需要:
spawn(fun() -> for(50) end)