Fwd: [erlang-bugs] Incorrect scope for a variable

Joe Armstrong erlang@REDACTED
Thu Feb 17 09:19:15 CET 2011


---------- Forwarded message ----------
From: Joe Armstrong <erlang@REDACTED>
Date: Thu, Feb 17, 2011 at 9:18 AM
Subject: Re: [erlang-bugs] Incorrect scope for a variable
To: Alexander Demidenko <alex.demidenko@REDACTED>


On Thu, Feb 17, 2011 at 8:54 AM, Alexander Demidenko <
alex.demidenko@REDACTED> wrote:

> Dear friends!
>
> Let me explain with an synthetic example.
>
> Erlang R13B04 (erts-5.7.5) [source] [smp:2:2] [rq:2] [async-threads:0]
> [kernel-poll:false]
> Eshell V5.7.5  (abort with ^G)
>
> 1> {case 1 of X -> X end, case 2 of X -> X; _ -> 4 end}.
> ** exception error: no match of right hand side value 1
>
>
This is not an error. Try this:

> {case 1 of X -> X end, case 2 of Y -> Y end}.
{1,2}

In evaluating {A, B}

any bindings created in A or B are merged, and the common value propagated
to the next lines of code.

In your example

{case 1 of X -> X end ,...}

evaluates to {1, ...} and has the side effect of binding X to 1

{... case 2 of X -> X end}


evaluates to {, ... 2} and has the side effect of binding X to 2

Now you try to merge the bindings X=1 and X=2 which fails



>
> opposite successful example with bit change:
> 2> f().
> ok
> 3> begin X=1, {case 1 of X -> X; _ -> 3 end, case 2 of X -> X; _ -> 4 end}
> end.
> {1,4}
>
>
You never bind X inside the tuple, so there is no merging



> In first example, erlang assume X=2 in second 'case' (why?) and trow
> exception before return tuple.
>
>


> I'm think this is incorrect behavior. Isn't it?
>
>
No - though on first sight it looked like bug, so I had to think a bit

/Joe


> --
> ---------------------------------------------
> With best regards,
> Alexander.
>
> ________________________________________________________________
> erlang-bugs (at) erlang.org mailing list.
> See http://www.erlang.org/faq.html
> To unsubscribe; mailto:erlang-bugs-unsubscribe@REDACTED
>
>


More information about the erlang-questions mailing list