[erlang-questions] Fw: Newbie question

Alpar Juttner ajuttner.list@REDACTED
Tue Jul 21 18:59:14 CEST 2009


Hi, 
> queue ! {put, "ABC"}.
> queue ! {put, "DEF"}.
> queue ! {put, "GHI"}.
> queue ! {get, self()}.
> receive
> {data, Data} -> io:format("Got ~s~n", [Data]);
> empty -> io:format("Got empty~n");
> AnyMessage -> io:format("Trap ~s~n", [AnyMessage]);
> _ -> io:format("Got something else~n")
> after 5000 -> io:format("timed out~n")
> end.
> 
> And I get "Got ABC".  I do another get and the same receive as above and I
> get:
> 
> ** exception exit: {badarg,[{io,format,[<0.31.0>,"Trap
> ~s~n",[{data,"DEF"}]]},
>                             {erl_eval,do_apply,5},
>                             {shell,exprs,6},
>                             {shell,eval_exprs,6},
>                             {shell,eval_loop,3}]}
>      in function  io:o_request/3
> 
> 
> I don't understand why the {data, Data} pattern isn't receiving the
> message.  Can someone help me understand this?

It is because Data is already bound to "ABC", so you try to match
against {data, "ABC"}.

Regards,
Alpar




More information about the erlang-questions mailing list