Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does in ? operator/prefix in Erlang mean?

Tags:

erlang

What does the question mark in ?MODULE (which can be seen in all generated code by Mochiweb make command) mean?

-export([start/1, stop/0, loop/2]).

start(Options) ->
    {DocRoot, Options1} = get_option(docroot, Options),
       Loop = fun (Req) ->
               ?MODULE:loop(Req, DocRoot)
       end,
    mochiweb_http:start([{name, ?MODULE}, {loop, Loop} | Options1]).

stop() ->
    mochiweb_http:stop(?MODULE).

loop(Req, DocRoot) ->
    ...
like image 547
Kevin Le - Khnle Avatar asked Sep 15 '11 11:09

Kevin Le - Khnle


2 Answers

It denotes a preprocessor macro. ?MODULE is one of the predefined macro constants that expand to current module's name.

like image 153
Cat Plus Plus Avatar answered Oct 18 '22 03:10

Cat Plus Plus


Well this is the way we represent MACROS in Erlang. At compile time, these macros are replaced with the actual meanings. They save on re-writing pieces of code or on abstracting out a parameter you may change in future without changing your code (would only require a re-compilation of the source that depends on the MACRO).

Forexample:

-module(square_plus).
-compile(export_all).
-define(SQUARE(X),X * X).

add_to_square(This,Number)-> ?SQUARE(This) + Number.

Is the same as:

-module(square_plus).
-compile(export_all).

add_to_square(This,Number)-> (This * This) + Number.
like image 26
Muzaaya Joshua Avatar answered Oct 18 '22 01:10

Muzaaya Joshua