3
我在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",[]}]}
它是否適合導入的函數? –
從'spawn(fun() - > ... end)調用導入的函數應該可以工作。三參數版本不會從導入函數中獲得任何東西 - 您需要指定原始模塊名稱。無論如何,最好避免在Erlang中導入函數,請參閱[本答案](https://stackoverflow.com/a/36092365/113848)。 – legoscia