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

Joe Armstrong erlang@REDACTED
Thu Feb 17 20:35:29 CET 2011


On Thu, Feb 17, 2011 at 3:59 PM, Ola Andersson A <
ola.a.andersson@REDACTED> wrote:

> Yes, that is what I would have expected too until I read Joes explanation
> below regarding the same statement in the shell.
>
> The question is why the same statement gives a different result in the
> shell as compared to when used in a module:
>

Very strange. Consider this module:

-module(a).
-compile(export_all).

foo() ->
    {case 1 of X -> X end, case 2 of X -> X end}.

foo1() ->
    {case 1 of X -> X end, case 2 of X -> X; _ -> 4 end}.

> a:foo().
** exception error: no case clause matching 2
     in function  a:foo/0
> a:foo1().
{1,4}

The second example shows that order of evaluation withing the tuple
is left-to-right, if it had been parallel then the result should have been
{1,2}
(so my earlier explanation was wrong).

In the shell - both expressions result in errors

> f().
ok
>  {case 1 of X -> X end, case 2 of X -> X end}.
** exception error: no match of right hand side value 1
> f().
ok
>  {case 1 of X -> X end, case 2 of X -> X; _ -> 4 end}.
** exception error: no match of right hand side value 1

There is no way I can explain this - the compiled version looks correct and
the version in the shell an error.

/Joe





Eshell V5.8.1  (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
> 2>
>
> In the shell X is not exported from the first case statement as it is in
> the function below.
> /OLA.
>
> > Greetings,
> >
> > Perhaps I am missing your point here, but if the first case
> > exports X as 1, then the second case should return 4 (as it does).
> >
> >
> > bengt
> >
> > On Thu, 2011-02-17 at 15:02 +0100, Ola Andersson A wrote:
> > > Maybe I'm just being tired now but how come this works when
> > used in a module?
> > >
> > > foo() -> {case 1 of X -> X end, case 2 of X -> X; _ -> 4 end}.
> > >
> > > This function nicely returns {1,4}.
> > >
> > > In this case the X is already bound to 1 when executing the
> > second case statement.
> > > Not so in the shell as mentioned below.
> > > /OLA.
> > >
> > > > -----Original Message-----
> > > > From: erlang-questions@REDACTED
> > > > [mailto:erlang-questions@REDACTED] On Behalf Of Raimo Niskanen
> > > > Sent: den 17 februari 2011 12:05
> > > > To: erlang-questions@REDACTED
> > > > Subject: Re: [erlang-questions] Fwd: [erlang-bugs]
> > Incorrect scope
> > > > for a variable
> > > >
> > > > On Thu, Feb 17, 2011 at 11:17:33AM +0100, Ola Andersson A wrote:
> > > > > Are you sure about the example?
> > > > >
> > > > > > > 1> {case 1 of X -> X end, case 2 of X -> X; _ -> 4 end}.
> > > > > > > ** exception error: no match of right hand side value 1
> > > > >
> > > > > I get:
> > > > > Erlang R13A (erts-5.7) [smp:2:2] [rq:2] [async-threads:0]
> > > > >
> > > > > Eshell V5.7  (abort with ^G)
> > > > > 1> case 1 of X -> X end, case 2 of X -> X; _-> 4 end.
> > > > > 4
> > > >
> > > > X was exported by the line above so now X is bound.
> > > >
> > > > > 2> {case 1 of X -> X end, case 2 of X -> X; _-> 4 end}.
> > > > > {1,4}
> > > >
> > > > Therefore that succeeded.
> > > >
> > > > Try
> > > >  1b> X.
> > > >  1c> f(X).
> > > > in between and watch the difference.
> > > >
> > > > > 3>
> > > > >
> > > > > Another thing that I find strange is that if I use the above
> > > > > constructs in a module I get a warning in the first case
> > > > but not in the second.
> > > > >
> > > > > 8> c(warn).
> > > > > ./warn.erl:12: Warning: variable 'X' exported from
> > 'case' (line 7)
> > > > > {ok,warn}
> > > > >
> > > > > I think I should get a warning for both constructs.
> > > > >
> > > > > This export from case thing is something I have never liked.
> > > > > I know that there are different opinions about this but I
> > > > would prefer
> > > > > a compile error for using the variable X outside of the
> > first case
> > > > > statement.
> > > > > /OLA.
> > > > >
> > > > > > ---------- 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
> > > > > > > 3> 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
> > > > > > >
> > > > > > >
> > > > > >
> > > > > ________________________________________________________________
> > > > > erlang-questions (at) erlang.org mailing list.
> > > > > See http://www.erlang.org/faq.html To unsubscribe;
> > > > > mailto:erlang-questions-unsubscribe@REDACTED
> > > > >
> > > >
> > > > --
> > > >
> > > > / Raimo Niskanen, Erlang/OTP, Ericsson AB
> > > >
> > > > ________________________________________________________________
> > > > erlang-questions (at) erlang.org mailing list.
> > > > See http://www.erlang.org/faq.html
> > > > To unsubscribe; mailto:erlang-questions-unsubscribe@REDACTED
> > > >
> > > >
> > > ________________________________________________________________
> > > erlang-questions (at) erlang.org mailing list.
> > > See http://www.erlang.org/faq.html
> > > To unsubscribe; mailto:erlang-questions-unsubscribe@REDACTED
> > >
> >
> >
> > ________________________________________________________________
> > erlang-questions (at) erlang.org mailing list.
> > See http://www.erlang.org/faq.html
> > To unsubscribe; mailto:erlang-questions-unsubscribe@REDACTED
> >
> >
> ________________________________________________________________
> erlang-questions (at) erlang.org mailing list.
> See http://www.erlang.org/faq.html
> To unsubscribe; mailto:erlang-questions-unsubscribe@REDACTED
>
>


More information about the erlang-questions mailing list