Erlang Programming/Macros

From Wikibooks, open books for an open world
Jump to navigation Jump to search

Macros[edit | edit source]

-define(LIKERT_SCALE, lists:seq(1, 5)). 

A = ?LIKERT_SCALE.

The code makes A = [1,2,3,4,5].

Some handy predefined macros include:

 ?MODULE  (module name)
 ?LINE    (line number)
 ?FILE    (filename as a string)
 % ===========================================
 % Example code
 % ===========================================
 -module(test_macros).
 -define(LIKERT_SCALE, lists:seq(1, 5)).
 -compile(export_all).
 
 start() ->
   io:format("likert scale:  ~w \n", [?LIKERT_SCALE]),
   io:format("module name:   ~w \n", [?MODULE]),
   io:format("line number:   ~w \n", [?LINE]),
   io:format("filename:      ~s \n", [?FILE]),
   ok.
 % ===========================================
 % Example output
 % ===========================================
 %
 % 6> c(test_macros).
 % {ok,test_macros}
 % 7> test_macros:start().
 % likert scale:  [1,2,3,4,5] 
 % module name:   test_macros 
 % line number:   10 
 % filename:      ./test_macros.erl
 % ok