[erlang-questions] confusing message behavior

Steven Grady <>
Wed Oct 22 21:55:13 CEST 2008


D'oh!  Of course.  I keep on expecting the scope of a variable binding  
in a receive to be restricted to the receive block.  Sigh.

Thanks,

	Steven

On Oct 22, 2008, at 12:51 PM, t ty wrote:

> After the first 'receive' X is bound to 'a', meaning the second
> receive could be viewed as:
>
> receive 'a' -> 'a' after 100 -> timeout end.
>
> Do the following:
>
> 1> test:m().
> 2> receive X -> X after 100 -> timeout end.
> 3> f(X)
> 4> receive X -> X after 100 -> timeout end.
>
> t
>
> On Wed, Oct 22, 2008 at 3:20 PM, Steven Grady <> wrote:
>> Can someone explain why some messages are thrown away when doing a
>> "receive" from the command line?
>>
>> $ cat test.erl
>> -module(test).
>> -export([m/0, g/0]).
>>
>> m() -> self() ! a, self() ! b.
>> g() -> receive X -> X after 100 -> timeout end.
>>
>> Bad behavior ("a" is received, but not "b"):
>>
>> $ erl
>> Erlang (BEAM) emulator version 5.6.2 [source] [async-threads:0]
>> [kernel-poll:false]
>>
>> Eshell V5.6.2  (abort with ^G)
>> 1> test:m().
>> b
>> 2> receive X -> X after 100 -> timeout end.
>> a
>> 3> receive X -> X after 100 -> timeout end.
>> timeout
>> 4>
>>
>> Good behavior (both messages are received):
>>
>> $ erl
>> Erlang (BEAM) emulator version 5.6.2 [source] [async-threads:0]
>> [kernel-poll:false]
>>
>> Eshell V5.6.2  (abort with ^G)
>> 1> test:m().
>> b
>> 2> test:g().
>> a
>> 3> test:g().
>> b
>> 4> test:g().
>> timeout
>> 5>
>>
>>       Steven
>>
>> _______________________________________________
>> erlang-questions mailing list
>> 
>> http://www.erlang.org/mailman/listinfo/erlang-questions
>>




More information about the erlang-questions mailing list