Sometimes you might want to have a single library using specific API depending on the platform it is compiled on. At some other time, you might want to do some optimizations only if you turn a flag ON. For all that, you can use conditional compilation macros (AKA preprocessor macros).
Note that #define doesn't exist; for that, learn about Haxe Macros.
Here's an example of multiplaform code:
There is also a not prefix, which is the exclamation mark. So for a block that is compiled for every plaform except for PHP, you can do:
#if !php // This code is not for PHP! #end
In addition to the various platform defines, you can also detect if this is a debug-enabled build (set using -debug):
#if debug trace("Debug infos for all debug compiles"); #end
You can define your own variables by using the Haxe compiler commandline options (for example using -D mydebug):
#if mydebug trace("Some personalized debug infos"); #end
Logical and/or operators are also supported:
#if (neko && mydebug) // Only for "mydebug" mode on Neko #end #if (flash || php) // Code that works for either flash or PHP #end
You can compile code based on an haxe compiler version :
#if haxe_210 trace(" haxe version >= 2.10"); #else trace(" haxe version < 2.10"); #end