Erlang:问号语法是什么意思? [英] Erlang: What does question mark syntax mean?
问题描述
Erlang语法中的问号是什么意思?
What does the question mark in Erlang syntax mean?
例如:
Json = ?record_to_json(artist, Artist).
源的完整上下文可以在此处。
The full context of the source can be found here.
推荐答案
Erlang使用问号来标识宏。例如考虑下面的代码:
Erlang uses question mark to identify macros. For e.g. consider the below code:
-ifdef(debug).
-define(DEBUG(Format, Args), io:format(Format, Args)).
-else.
-define(DEBUG(Format, Args), void).
-endif.
如文档所述,
在编译过程中会扩展宏。一个简单的宏
?Const
将替换为Replacement
。
此代码段定义了一个名为 DEBUG
的宏,该宏将替换为在 debug时打印字符串的调用
在编译时设置。然后在下面的代码中使用该宏:
This snippet defines a macro called DEBUG
that is replaced with a call to print a string if debug
is set at compile time. The macro is then used in the following code thus:
?DEBUG("Creating ~p for N = ~p~n", [First, N]),
如果调试
已设置。因此,只有设置 debug
时,您才能看到调试消息。
This statement is expanded and replaced with the appropriate contents if debug
is set. Therefore you get to see debug messages only if debug
is set.
更新
感谢 @ rvirding :
问号表示尝试扩展以下内容宏调用。没有什么可以禁止将宏名称(原子或变量)用作常规原子或变量。因此,在上述示例中,只要不使用
作为前缀,就可以使用
DEBUG
作为普通变量? $ c>。最令人困惑的是,但绝对不是非法的。
A question mark means to try and expand what follows as a macro call. There is nothing prohibiting using the macro name (atom or variable) as a normal atom or variable. So in [the above] example you could use
DEBUG
as a normal variable just as long as you don't prefix it with?
. Confusing, most definitely, but not illegal.
这篇关于Erlang:问号语法是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!