Conditional compilation

haXe allows you to do conditional compilation. This means that some parts of the code can be compiled and others can be ignored depending on the flags you give to the compiler and depending on the platform you're targeting.

Conditional compilation instructions always begin with the #if instruction.

Conditional compilation depending on flags

You can define a flag on the command line by using the -D switch. So, to define the myway flag, you would use the following: -D myway.

Then, you use it in the following way:

#if myway //Code here compiled only if myway is defined #else //Code here compiled in the other case #end

There is also a not operator:

#if !myway //Code here compiled only myway is not defined #end

Conditional compilation depending on the target

Conditional compilation depending on the target basically works in the same way, except that the name of the target you are compiling to automatically gets set. So, you will have something like the following:

#if cpp //C++ code #elseif neko //Neko code #elseif php //PHP code #else //Code for other targets #end

By the way, did you notice the presence of #elseif in the code? It could prove to be pretty useful if you need to create a different implementation of the same feature depending on the platform.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.221.163.13