[erlang-questions] case expression scope

Daniel Goertzen daniel.goertzen@REDACTED
Mon Mar 3 20:07:36 CET 2014


Hmm, fun wrapping could be the basis of hygienic-case parse transform.
I'll have to see what the performance overhead of a fun wrap is like.

The example I gave is contrived and a bit weak I guess.  In my most recent
run-in with this issue there were a lot of bindings from the enclosing
scope that were used, so breaking things out to top level functions would
not always be a good option.  Funs would be better, but add clutter and
maybe runtime overhead.  Compromises, compromises....

Cheers,
Dan.


On Mon, Mar 3, 2014 at 12:46 PM, Thomas Lindgren
<thomasl_erlang@REDACTED>wrote:

> You could hide the bindings inside a fun.
>
>    A = (fun() -> case ... end end)( ),
>    B = (fun() -> case ... end end)( ),
>    {A, B}.
>
> Though I agree with Ivan that refactoring might be a better idea in this
> case.
>
> A related issue: I'd like to get warnings when variables are matched (not
> bound) in patterns. I've seldom found repeated variables very useful in
> practice, they can easily be rewritten to use explicit compares, and
> sometimes they have hidden groan-inducing bugs.
>
> (Binary patterns are a special case; _some_ repeated variables are be
> benign there.)
>
> Best,
> Thomas
>
>
>   On Monday, March 3, 2014 5:59 PM, Daniel Goertzen <
> daniel.goertzen@REDACTED> wrote:
>
> One thing that repeatedly bites me is the way variables bindings leak out
> of case expressions.  For example, the following code fails to even compile
> because the Ps and Qs smack into each other...
>
> f1(X, Y) ->
>     A = case {X,Y} of
>             {0, Q} -> Q;
>             {P, Q} -> P*Q
>         end,
>
>     B = case {X,Y} of
>             {0, Q} -> Q;
>             {P, Q} -> P+Q
>         end,
>     {A,B}.
>
>
> In situations like the above, I artificially change the variable names to
> avoid trouble and end up with something like this:
>
> f1(X, Y) ->
>     A = case {X,Y} of
>             {0, Q0} -> Q0;
>             {P0, Q0} -> P0*Q0
>         end,
>
>     B = case {X,Y} of
>             {0, Q1} -> Q1;
>             {P1, Q1} -> P1+Q1
>         end,
>     {A,B}.
>
>
> The binding leakages seems like nothing but hassles and headaches to me,
> so my questions is, is there actually a reason why bindings need leak out
> of case expressions?  Is there a programming style that leverages this
> feature?  Is there a limitation in the emulator that forces it to be this
> way?
>
> I'd love to have a 'hygienic' case expression.  Could it be faked with
> parse transforms?
>
> Dan.
>
>
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20140303/a9e9af7d/attachment.htm>


More information about the erlang-questions mailing list