Search code examples
delphidelphi-xe6

How can I use Macros in Delphi?


How can I use Macros in Delphi? This feature is available in FPC for quite some time now. Could it be done with pre or post build events?

Example:

{$define sum:=a:=a+b;}  

Solution

  • You cannot use macros in Delphi. No such functionality exists. The closest built in functionality would be an inline function.


    You could certainly write your own pre-processor that expands macros. However, you won't be able to make that integrate well with the IDE. At least, not in a way that I would consider acceptable.

    The problem is where to expand the macro to. You can hardly expand it to the original file, because you cannot realistically modify that. So you'd need to expand to a temporary file. And the compiler doesn't have any mechanism to build from a temporary files. I suppose you might expand to an include file. That may be the best option but it's going to be horrible to work with. For instance, consider the fun when debugging.