[erlang-questions] Fwd: [erlang-bugs] Incorrect scope for a variable
Ola Andersson A
ola.a.andersson@REDACTED
Thu Feb 17 15:02:43 CET 2011
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
>
>
More information about the erlang-questions
mailing list