Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Erlang macro


May 13, 2021 Erlang


Table of contents


Erlang macro

Another thing added to the messager system is the macro. I nclude mess_config definition in the .hrl file:

%%% Configure the location of the server node,
-define(server_node, messenger@super).

This header file is included in mess_server.erl file:

-include("mess_config.hrl").

Thus, each mess_server that appears in server_node.erl is replaced with messenger@super.

Macros are also used to build service-side processes:

spawn(?MODULE, server, [])

This is a standard macro (that is, it is a system-defined macro rather than a user-defined macro). T he MODULE macro is always replaced with the current module name (that is, the name defined by -module at the beginning of the file). M acros have many advanced uses, and being an argument is just one of them.

The three Erlang (.erl) files in the Messager system are distributed and compiled into three separate target code files (.beam). W hen these codes are referenced during execution, they are loaded and linked to the system. I n this case, we put them all under the current working directory (that is, the directory where you are after you execute the "cd" command). W e can also put these files in other directories.

In this messager example, we do not make any assumptions or restrictions about what is sent. These messages can be any legitimate Erlang item.