HaxeDoc2

(やまだぃちぅ) #1

Chapter 6


Language Features


6.1 Conditional Compilation..................................


Haxe allows conditional compilation by using#if,#elseifand#elseand checking forcom-
piler flags.

Definition: Compiler Flag
A compiler flag is a configurable value which may influence the compilation process. Such
a flag can be set by invoking the command line with-D key=valueor just-D key, in
which case the value defaults to"1". The compiler also sets several flags internally to pass
information between different compilation steps.

This example demonstrates usage of conditional compilation:
1 class ConditionalCompilation {
2 public static function main(){
3 #if !debug
4 trace("ok");
5 #elseif (debug_level > 3)
6 trace(3);
7 #else
8 trace("debug level too low");
9 #end
10 }
11 }


Compiling this without any flags will leave only thetrace("ok");line in the body of the
mainmethod. The other branches are discarded while parsing the file. As a consequence, these
branches must still contain valid Haxe syntax, but the code is not type-checked.
The conditions after#ifand#elseifallow the following expressions:


  • Any identifier is replaced by the value of the compiler flag by the same name. Note that
    -D some-flagfrom command line leads to the flagssome-flagandsome_flagto be
    defined.

  • The values ofString,IntandFloatconstants are used directly.

  • The boolean operators&&(and),||(or) and!(not) work as expected.

Free download pdf