Conditional Compilation

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):

Here's an example of multiplaform code:

    #if flash8
    // haXe code specific for flash player 8
    #elseif flash
    // haXe code specific for flash platform (any version)
    #elseif js
    // haXe code specific for javascript plaform
    #elseif neko
    // haXe code specific for neko plaform
    #else 
    // do something else
        #error  // will display an error "Not implemented on this platform"
    #end

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

Here's another example for turning on some logs only if mydebug flag is used when compiling the code (using -D mydebug):

    #if mydebug
    trace("Some debug infos");
    #end

You can define your own variables by using the haXe compiler commandline options.

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

«« Optional Arguments | Inline »»

version #6937, modified 2009-09-04 07:51:53 by Abscissa