Search code examples
macrospascalfreepascalpreprocessor

Pass parameter in pascal define


I'm trying to define something in pascal (freepascal). As in c++ you can pass variable define macro like this :

#define REP(i,k) for(int i=0; i<k; i++)

How can you do it in pascal?

I have added {$MACRO ON} command on the first line, and as a result it can run normal define without error like {$define lala:='hello world'}.

But when I try {define lala(i):=i} the program got an error.

How can I pass variable parameter in Pascal define?


Solution

  • Use an external preprocessor, and execute it in the build system before you use the Pascal compiler on the result.

    The FPC macro system is not meant for meta programming but for calling convention macros in headers and compact enable/disable logic only (like home-brewn assert and other debug logging code) and doesn't support parametrisation.

    Non hygienic macros are fundamentally incompatible with the Pascal unit system, and hygienic macros are covered by inline function/procedures.