25

What does the question mark in Erlang syntax mean?

For example:

Json = ?record_to_json(artist, Artist).

The full context of the source can be found here.

Brian Campbell
  • 322,767
  • 57
  • 360
  • 340
Ted Karmel
  • 1,046
  • 1
  • 12
  • 20
  • Used google but didn't come up with anything ( for: "erlang question mark syntax"). Maybe I should activate web history or something to get more relevant results (even using the same keywords results are not the same for everybody especially on more idiosyncratic keywords). but thanks for comment... still i would appreciate someone with erlang experience providing some more insightful context on how and why used... – Ted Karmel Sep 18 '10 at 14:16
  • 14
    @delnan - Googling brings up this question – OrangeDog May 12 '11 at 11:24

3 Answers3

30

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.

As the documentation says,

Macros are expanded during compilation. A simple macro ?Const will be replaced with Replacement.

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]),

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.

Update

Thanks to @rvirding:

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.

Community
  • 1
  • 1
Manoj Govindan
  • 72,339
  • 21
  • 134
  • 141
  • 4
    To be picky: 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 your 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. – rvirding Sep 19 '10 at 11:25
  • @rvirding: Thanks! Have shamelessly borrowed it and added to my answer. – Manoj Govindan Sep 19 '10 at 14:38
5

Based on this documentation, I believe it's the syntax for referring to a macro.

And from Learn You Some Erlang:

Erlang macros are really similar to C's '#define' statements, mainly used to define short functions and constants. They are simple expressions represented by text that will be replaced before the code is compiled for the VM. Such macros are mainly useful to avoid having magic values floating around your modules. A macro is defined as a module attribute of the form: -define(MACRO, some_value). and is used as ?MACRO inside any function defined in the module. A 'function' macro could be written as -define(sub(X,Y), X-Y). and used like ?sub(23,47), later replaced by 23-47 by the compiler. Some people will use more complex macros, but the basic syntax stays the same.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
0

? indicates Macros.

Erlang has predefined macros as well. Example: ?MODULE. You can find the complete list in https://www.erlang.org/doc/reference_manual/macros.html#predefined-macros

Mohan
  • 129
  • 8