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

综合编程 2017-04-11

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'

责编内容by:Medium (源链)。感谢您的支持!


How to build a self-sustaining development team This is an excerpt from The route to the successful adoption of non-ma...
kvakvs/ErlangRT ErlangRT - Runtime Erlang Replacement Therapy. Another attempt to make Erla...
Leibniz is a pure Erlang/Elixir math expression pa... Leibniz Leibniz is a pure erlang/elixir math expression parser and evaluato...
In Defense of `gen_event` In Defense of gen_event 2018-05-22 :: ...
Erlang并发进阶 Getting Started with Erlang User’s Guide Version 9.0 写在前面的话 ...