Aaargh!! - Macros, Parse Transforms and others @ Erlang Battleground

Datetime:2017-04-11 05:18:40         Topic: Erlang          Share        Original >>
Here to See The Original Article!!!

The Weird Module

This is basically the module that John sent to the mailing list (I changed its name just so it’s easier to avoid the apostrophes)…

-compile({parse_transform, undefined_parse_transform}).

The idea is that we first define the PLEASE_DONT macro in a line that we can later comment out if needed (or rather remove and define the macro at compile time).

Then, if the macro is not defined, we run the code through a parse transformation called undefined_parse_transform . The key point here is that this parse transformation module doesn’t exist, so this line should not compile .

But the macro is defined, so the parse transform should not be used, right?

$ erlc weird.erl
src/weird.erl: undefined parse transform 'undefined_parse_transform'

And My Macro?

Turns out that macro is not defined. As Alex points out in the mailing list…

there’s no one-argument define()

As you can see in the docs :

A macro is defined as follows:

-define(Const, Replacement).

-define(Func(Var1,...,VarN), Replacement).

If we change the define line in our code…

-define(PLEASE_DONT, true).
-compile({parse_transform, undefined_parse_transform}).

…it compiles perfectly:

$ erlc weird.erl
$ ls weird.beam

Why didn’t you tell me so?

That’s it, right? Well… not so easy. If there is no one-argument define(), then why did the compiler/parser not warned us about it. Check this out:


If we try to compile that module…

$ erlc weird.erl
src/weird.erl:3: badly formed 'define'


Put your ads here, just $200 per month.