代码产生的错误是
2> X = "2".
"2"
3> case_:main(X).
main 50
sender 50
** exception error: bad argument
in function case_:sender/1 (case_.erl, line 14)
in call from case_:main/1 (case_.erl, line 6)
4> Z = 2.
2
5> case_:main(Z).
** exception error: bad argument
in function io:format/3
called as io:format(<0.25.0>,"main ~p~n",2)
in call from case_:main/1 (case_.erl, line 5)
6>
在第一次尝试中,我尝试传递一个字符串,这比第二次尝试传递一个整数要远得多。我不确定为什么这不起作用。
函数调用sender(Input)
应该{Data}
从receiver()
函数调用返回。
我肯定需要程序的消息传递部分,因为我正在尝试编写一个循环来接收消息,评估它们并返回结果;但也许该case...of
声明可能会被丢弃。
-module(case_).
-export([main/1, sender/1, receiver/0]).
main(Input) ->
io:format("main ~p~n",Input),
case sender(Input) of
{Data} ->
io:format("Received ~p~n",Data)
end.
sender(Input) ->
io:format("sender ~p~n",Input),
Ref = make_ref(),
?MODULE ! { self(), Ref, {send_data, Input}},
receive
{Ref, ok, Data} ->
{Data}
end.
receiver() ->
io:format("receiver ~n"),
receive
{Pid, Ref, {send_data, Input}} ->
Pid ! { Ref, ok, Input + Input}
end.