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

综合编程 Medium

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'
Medium稿源:Medium (源链) | 关于 | 阅读提示

本站遵循[CC BY-NC-SA 4.0]。如您有版权、意见投诉等问题,请通过eMail联系我们处理。
酷辣虫 » 综合编程 » Aaargh!! – Macros, Parse Transforms and others @ Erlang Battleground

喜欢 (0)or分享给?

专业 x 专注 x 聚合 x 分享 CC BY-NC-SA 4.0

使用声明 | 英豪名录