[erlang-questions] Defining records using macros

Richard Carlsson carlsson.richard@REDACTED
Thu Jul 18 09:33:59 CEST 2013

On 2013-07-18 09:15 , Yash Ganthe wrote:
> -define(REC_DEFINE(Type),
>                  -record(Type, {
>        id
>        ,val
>      }).       %% The . is an essential part of record definition
> ).  %% Complains with syntax error: syntax error before: ')'
> My intention is to use a macro for defining records.
> ?REC_DEFINE(my_rec).
> Is there an escape character that the preprocessor can be give for the dot?

The preprocessor (just like the compiler) works on units of whole 
"forms", i.e., dot-terminated token sequences. Since the define must 
also be a whole form, it will end at the first dot token and there is no 
way to escape it.

But you need to write your macro instantiations with a dot terminator 
anyway (just as you did above, in fact), otherwise the preprocessor 
won't have a unit to work on. Thus, you don't need to supply the dot in 
the definition, and the following works:


-define(R(T), -record(T, {a, b})).


f() ->
     #foo{a=A1,b=B1} = #foo{},
     #bar{a=A2,b=B2} = #bar{}.


More information about the erlang-questions mailing list