[erlang-questions] Defining records using macros

Manuel A. Rubio "Bombadil" bombadil@REDACTED
Thu Jul 18 09:37:10 CEST 2013


Hi Yash,

El 2013-07-18 09:15, Yash Ganthe escribió:
> -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?

I wrote a test:

-------
-module(test).
-compile([export_all]).

-define(REC_DEFINE(Type),
     -record(Type, {id, val})
).

?REC_DEFINE(one).
?REC_DEFINE(two).
?REC_DEFINE(three).

testing() ->
         #one{},
         #two{},
         #three{},
         ok.
--------

Works well... if you want to include record definition inside a 
function, is not possible with or without a macro. A macro is only a 
"rewritter", by example:

--------
-define('->', :).

main() ->
     test ?'->' testing().
---------

If you want to do something with the erlang syntax, I think you should 
review the parse_transform way to do it :-)

Regards.
Manuel Rubio.



More information about the erlang-questions mailing list