[erlang-questions] if-elseif-else trick

Peti Gömöri gomoripeti@REDACTED
Tue Feb 19 10:48:22 CET 2019


This makes much more sense now :) Just noting that Elixir has the `cond`
expression for this use case.

On Tue, Feb 19, 2019 at 8:37 AM Viktor Söderqvist <viktor@REDACTED>
wrote:

> Hi!
>
> My example proved silly. Of course, normal 'if' is fine for guard
> expressions and catch-all is to be avoided in general. I agree.
>
> What I failed to mention was that you can use non-guard expressions with
> this trick. (The trick's actually rewriting the elseifs to nested case.)
>
> When using if with non-guard expressions, you need to evaluate them in
> advance, which you may not want if these are side-effectful or just slow:
>
> f() ->
>     Cond1 = g1(),
>     Cond2 = g2(),
>     Cond3 = g3(),
>     if Cond1 -> a;
>        Cond2 -> b;
>        Cond3 -> c;
>        true  -> d
>     end.
>
> Therefore, you often see nested case expressions instead:
>
> f() ->
>     case g1() of
>         true -> a;
>         false ->
>             case g2() of
>                 true -> b;
>                 false ->
>                     case g3() of
>                         true -> c;
>                         false -> d
>                     end
>             end
>     end.
>
> ... or broken down into multiple functions:
>
> f() ->
>     case g1() of
>         true -> a;
>         false -> f2()
>     end.
> f2() ->
>     %% You know
>
> There's the throw style too, a slightly silly but a flat style:
>
> f() ->
>     try
>         g1() andalso throw(a);
>         g2() andalso throw(b);
>         g3() andalso throw(c);
>         d
>     catch
>         X -> X
>     end.
>
> The point of the ?elseif syntax trick is that it lets you write the
> nested case above as the flat structure:
>
> f() ->
>     ?'if'(g1())   -> a;
>     ?elseif(g2()) -> b;
>     ?elseif(g3()) -> c;
>     ?else         -> d.
>
> You can even bind variables in the conditions if you would want that,
> just to avoid deeply nested code. I would look more like perl than
> erlang though, so don't do that.
>
> >> That said...
> >> The original premise of this thread is based on a misunderstanding of
> `if` and probably inexperience with `case`.
> >
> > Agreed
> >
>
> I agree too. :-) Thanks for clarifying!
>
> Btw, I'm sorry for posting without having a real question. I just wanted
> to show what can be done using a non-trivial parse transform.
>
> Viktor
> _______________________________________________
> 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/20190219/37963c68/attachment.htm>


More information about the erlang-questions mailing list