haXe

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
    #else flash
    // haXe code specific for flash platform (any version)
    #else js
    // haXe code specific for javascript plaform
    #else neko
    // haXe code specific for neko plaform
    #else error // will display an error "Not implemented on this platform"
    #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

Note that you cannot use #else with a variable afterwards. The following is incorrect:

    #if something
    // some code
    #else
    var i = 15;
    // something else
    #end

This will be interpreted as #else var and then you will get a syntax error.

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

«« Optional Arguments | Inline »»

version #1765, modified 2008-07-23 11:44:13 by techguy