<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class="">Hello Viktor,<div class=""><br class=""></div><div class="">You might look into my solution where this concept is generally solved under the abstraction of category pattern.<br class=""><div class=""><br class=""></div><div class=""><a href="https://github.com/fogfish/datum/blob/master/doc/category.md" class="">https://github.com/fogfish/datum/blob/master/doc/category.md</a></div><div class=""><br class=""></div><div class=""><br class=""></div><div class="">Best Regards,</div><div class="">Dmitry</div><div class=""><br class=""><div><br class=""><blockquote type="cite" class=""><div class="">On 19 Feb 2019, at 11.48, Peti Gömöri <<a href="mailto:gomoripeti@gmail.com" class="">gomoripeti@gmail.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class="">This makes much more sense now :) Just noting that Elixir has the `cond` expression for this use case.<br class=""></div><br class=""><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Tue, Feb 19, 2019 at 8:37 AM Viktor Söderqvist <<a href="mailto:viktor@zuiderkwast.se" class="">viktor@zuiderkwast.se</a>> wrote:<br class=""></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Hi!<br class="">
<br class="">
My example proved silly. Of course, normal 'if' is fine for guard<br class="">
expressions and catch-all is to be avoided in general. I agree.<br class="">
<br class="">
What I failed to mention was that you can use non-guard expressions with<br class="">
this trick. (The trick's actually rewriting the elseifs to nested case.)<br class="">
<br class="">
When using if with non-guard expressions, you need to evaluate them in<br class="">
advance, which you may not want if these are side-effectful or just slow:<br class="">
<br class="">
f() -><br class="">
Cond1 = g1(),<br class="">
Cond2 = g2(),<br class="">
Cond3 = g3(),<br class="">
if Cond1 -> a;<br class="">
Cond2 -> b;<br class="">
Cond3 -> c;<br class="">
true -> d<br class="">
end.<br class="">
<br class="">
Therefore, you often see nested case expressions instead:<br class="">
<br class="">
f() -><br class="">
case g1() of<br class="">
true -> a;<br class="">
false -><br class="">
case g2() of<br class="">
true -> b;<br class="">
false -><br class="">
case g3() of<br class="">
true -> c;<br class="">
false -> d<br class="">
end<br class="">
end<br class="">
end.<br class="">
<br class="">
... or broken down into multiple functions:<br class="">
<br class="">
f() -><br class="">
case g1() of<br class="">
true -> a;<br class="">
false -> f2()<br class="">
end.<br class="">
f2() -><br class="">
%% You know<br class="">
<br class="">
There's the throw style too, a slightly silly but a flat style:<br class="">
<br class="">
f() -><br class="">
try<br class="">
g1() andalso throw(a);<br class="">
g2() andalso throw(b);<br class="">
g3() andalso throw(c);<br class="">
d<br class="">
catch<br class="">
X -> X<br class="">
end.<br class="">
<br class="">
The point of the ?elseif syntax trick is that it lets you write the<br class="">
nested case above as the flat structure:<br class="">
<br class="">
f() -><br class="">
?'if'(g1()) -> a;<br class="">
?elseif(g2()) -> b;<br class="">
?elseif(g3()) -> c;<br class="">
?else -> d.<br class="">
<br class="">
You can even bind variables in the conditions if you would want that,<br class="">
just to avoid deeply nested code. I would look more like perl than<br class="">
erlang though, so don't do that.<br class="">
<br class="">
>> That said...<br class="">
>> The original premise of this thread is based on a misunderstanding of `if` and probably inexperience with `case`.<br class="">
> <br class="">
> Agreed<br class="">
> <br class="">
<br class="">
I agree too. :-) Thanks for clarifying!<br class="">
<br class="">
Btw, I'm sorry for posting without having a real question. I just wanted<br class="">
to show what can be done using a non-trivial parse transform.<br class="">
<br class="">
Viktor<br class="">
_______________________________________________<br class="">
erlang-questions mailing list<br class="">
<a href="mailto:erlang-questions@erlang.org" target="_blank" class="">erlang-questions@erlang.org</a><br class="">
<a href="http://erlang.org/mailman/listinfo/erlang-questions" rel="noreferrer" target="_blank" class="">http://erlang.org/mailman/listinfo/erlang-questions</a><br class="">
</blockquote></div>
_______________________________________________<br class="">erlang-questions mailing list<br class=""><a href="mailto:erlang-questions@erlang.org" class="">erlang-questions@erlang.org</a><br class="">http://erlang.org/mailman/listinfo/erlang-questions<br class=""></div></blockquote></div><br class=""></div></div></body></html>