[erlang-questions] if-elseif-else trick

Andreas Schultz andreas.schultz@REDACTED
Mon Feb 18 09:02:11 CET 2019


Hi Viktor,

Viktor Söderqvist <viktor@REDACTED> schrieb am Sa., 16. Feb. 2019 um
19:24 Uhr:

> Hey list!
>
> Last night I was thinking that it might be possible to implement
> if-elseif-else syntax using macros and a parse transform, so you can
> write like this:
>
> -include_lib("elseif/include/elseif.hrl").
>
> f(X) ->
>     ?'if'(X > 0)   -> pos
>     ?elseif(X < 0) -> neg
>     ?else          -> zero
>     end.
>

but why would you use such a thing when you can simply write:

if X > 0 -> pos;
   X < 0 -> neg;
   true -> zero
end.

Andreas


> It's possible. Here is how: https://github.com/zuiderkwast/elseif
>
> Disclaimer: This may be considered macro and parse-transform abuse. You
> may get strange syntax error messages if e.g. using ?elseif without
> ?'if' or if leaving out the mandatory ?else part.
>
> Viktor
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions
>
-- 
-- 
Dipl.-Inform. Andreas Schultz

----------------------- enabling your networks ----------------------
Travelping GmbH                     Phone:  +49-391-81 90 99 0
Roentgenstr. 13                     Fax:    +49-391-81 90 99 299
39108 Magdeburg                     Email:  info@REDACTED
GERMANY                             Web:    http://www.travelping.com

Company Registration: Amtsgericht Stendal        Reg No.:   HRB 10578
Geschaeftsfuehrer: Holger Winkelmann          VAT ID No.: DE236673780
---------------------------------------------------------------------
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20190218/dcbb89f0/attachment.htm>


More information about the erlang-questions mailing list