2
由代碼生成的誤差Erlang:case..of構造函數調用返回?
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)
應該從receiver()
函數調用中返回{Data}
。
我絕對需要程序的消息傳遞部分,因爲我試圖編寫一個需要消息的循環,對它們進行評估並返回結果;但也許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.