[erlang-questions] if-elseif-else trick

Viktor Söderqvist viktor@REDACTED
Sat Feb 16 19:24:38 CET 2019


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.

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



More information about the erlang-questions mailing list