A file can be included in the following way:
-include(File). -include_lib(File).
Include files are typically used for record and macro
definitions that are shared by several modules. It is
recommended that the file name extension
If the filename
Examples:
-include("my_records.hrl"). -include("incdir/my_records.hrl"). -include("/home/user/proj/my_records.hrl"). -include("$PROJ_ROOT/my_records.hrl").
-include_lib("kernel/include/file.hrl").
The code server uses
A macro is defined the following way:
-define(Const, Replacement).
-define(Func(Var1,...,VarN), Replacement).
A macro definition can be placed anywhere among the attributes and function declarations of a module, but the definition must come before any usage of the macro.
If a macro is used in several modules, it is recommended that the macro definition is placed in an include file.
A macro is used the following way:
?Const
?Func(Arg1,...,ArgN)
Macros are expanded during compilation. A simple macro
-define(TIMEOUT, 200).
...
call(Request) ->
server:call(refserver, Request, ?TIMEOUT).
This will be expanded to:
call(Request) ->
server:call(refserver, Request, 200).
A macro
-define(MACRO1(X, Y), {a, X, b, Y}).
...
bar(X) ->
?MACRO1(a, b),
?MACRO1(X, 123)
This will be expanded to:
bar(X) ->
{a,a,b,b},
{a,X,b,123}.
It is good programming practice, but not mandatory, to ensure that a macro definition is a valid Erlang syntactic form.
To view the result of macro expansion, a module can be compiled
with the
The following macros are predefined:
This section describes a feature introduced in R13B04. It remains fully compatible with old code but previous versions of erlang does not support the macros overloading.
It is possible to have more than one definition of the same macro, except for predefined macros. Only user-defined macros are considered here. In order to overload a macro, a different signature should be used for each overloaded version. A macro signature consists of its name and its number of arguments.
-define(M, io:format("object-like macro")).
-define(M(), io:format("function-like macro with 0 argument")).
-define(M(X), io:format("function-like macro with 1 argument: ~p", [X])).
-define(M(X, Y), io:format("function-like macro with 2 arguments: (~p, ~p)", [X,Y])).
An object-like macro should not be confused with a function-like macro with 0 argument.
When a macro is used, the preprocessor selects the proper macro following these rules:
-define(FUN, now).
...
call() ->
{?FUN, ?FUN()}.
This will be expended to:
call() ->
{now, now()}.
-define(MOD, erlang).
-define(MOD(X), X).
...
call() ->
?MOD:module_info().
This will be expended to:
call() ->
erlang:module_info().
-define(CALL(Fun), Fun()).
-define(CALL(Mod, Fun), Mod:Fun()).
...
call() ->
?CALL(kernel, module_info).
This will be expended to:
call() ->
kernel:module_info().
-define(M, erlang).
-define(M(X), X).
...
call() ->
?M(kernel, module_info).
The compiler will fail with error The following macro directives are supplied:
The macro directives cannot be used inside functions.
Example:
-module(m).
...
-ifdef(debug).
-define(LOG(X), io:format("{~p,~p}: ~p~n", [?MODULE,?LINE,X])).
-else.
-define(LOG(X), true).
-endif.
...
When trace output is desired,
% erlc -Ddebug m.erl or 1> c(m, {d, debug}). {ok,m}
The construction
The feature was added in Erlang 5.0/OTP R7.
Example:
-define(TESTCALL(Call), io:format("Call ~s: ~w~n", [??Call, Call])).
?TESTCALL(myfunction(1,2)),
?TESTCALL(you:function(2,1)).
results in
io:format("Call ~s: ~w~n",["myfunction ( 1 , 2 )",m:myfunction(1,2)]),
io:format("Call ~s: ~w~n",["you : function ( 2 , 1 )",you:function(2,1)]).
That is, a trace output with both the function called and the resulting value.