[erlang-questions] Use of unsafe variables...
Ola Andersson A
ola.a.andersson@REDACTED
Wed Nov 30 10:58:11 CET 2011
Hi,
I think both of the valid constructs below are rather ugly. Allowed, but ugly. A matter of taste of course.
There is however a third option available.
Whenever I need to use the return value of a case statement I make sure to have a period after the 'end'.
Like this:
test() ->
case lists:keyfind(ok, 1, []) of
{ok,[MISSING]} ->
{ok, MISSING};
false ->
{ok, 3}
end.
Yes, I like my functions short.
/OLA.
> -----Original Message-----
> From: erlang-questions-bounces@REDACTED
> [mailto:erlang-questions-bounces@REDACTED] On Behalf Of
> Raimo Niskanen
> Sent: den 28 november 2011 15:37
> To: erlang-questions@REDACTED
> Subject: Re: [erlang-questions] Use of unsafe variables...
>
> On Sat, Nov 26, 2011 at 08:55:11AM -0500, Reynaldo Baquerizo wrote:
> > Hi Antoine,
> >
> > > Is this correct or documented ?
> > > Thanks
> > >
> > > Take this module:
> > >
> > > -module(tsm_case).
> > > -export([test/0]).
> > >
> > > test() ->
> > > case lists:keyfind(ok, 1, []) of
> > > {ok,[_]} ->
> > > ok;
> > > false ->
> > > MISSING = 3
> > > end,
> > > {ok, MISSING}.
> > >
> > > Compile it:
> > > 31> c(tsm_case).
> > > ./tsm_case.erl:12: variable 'MISSING' unsafe in 'case' (line 6)
> > > error
> >
> > An excerpt from
> > http://www.erlang.org/doc/reference_manual/expressions.html
> >
> > """
> > The scope for a variable is its function clause. Variables
> bound in a
> > branch of an if, case, or receive expression must be bound in all
> > branches to have a value outside the expression, otherwise
> they will
> > be regarded as 'unsafe' outside the expression.
> > """
> >
> > > Now see the difference:
> > >
> > > -module(tsm_case).
> > > -export([test/0]).
> > >
> > > test() ->
> > > case lists:keyfind(ok, 1, []) of
> > > {ok,[MISSING]} ->
> > > ok;
> > > false ->
> > > MISSING = 3
> > > end,
> > > {ok, MISSING}.
>
> So, yes it is correct and documented. The variable MISSING is
> bound in all clauses of the case and hence can be used after the case.
>
> >
> > the correct form would be to bound MISSING to the return
> value of the
> > case expression (everything is an expression in Erlang)
>
> The code below is by most percieved as much easier to read
> but the code above is also correct. That MISSING is bound in
> the clause match makes it maybe harder to grasp but it is
> anyway bound.
>
> >
> >
> > test() ->
> > Val = case lists:keyfind(ok, 1, [{ok,23}]) of
> > {ok, MISSING} ->
> > MISSING;
> > false ->
> > 3
> > end,
> > {ok, Val}.
> > _______________________________________________
> > erlang-questions mailing list
> > erlang-questions@REDACTED
> > http://erlang.org/mailman/listinfo/erlang-questions
>
> --
>
> / Raimo Niskanen, Erlang/OTP, Ericsson AB
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions
>
More information about the erlang-questions
mailing list