Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Haxe defines with dot

In Haxe, what is the correct way to refer to a define with a dot?

For example, for the library thx.core, how to write conditional compilation against the library name?

#if thx.core

#end

Furthermore, are there general rules for special characters?

like image 383
KevinResoL Avatar asked Nov 16 '25 22:11

KevinResoL


2 Answers

The #if flag syntax does not seem to handle dots. However, Compiler.getDefine() handles most characters, including the dot:

hxml/build command: -D é'"(-è_.çà)=test

#if "é'\"(-è_çà)"
trace('will always be called, even without the -D');
#end

trace(haxe.macro.Compiler.getDefine("é'\"(-è_.çà)")); // test

There is a workaround for dots with initialization macros, even if it is not really pretty:

build.hxml

-x Main.hx
-D abc.def
--macro Macro.parseDefines()

Macro.hx

import haxe.macro.Compiler;

class Macro {
    public static macro function parseDefines():Void {
        if (Compiler.getDefine("abc.def") != null) {
            Compiler.define("abc_def");
        }
    }
}

Main.hx

class Main {
    public static function main() {
        #if abc_def
        trace("abc.def is defined!");
        #end
    }
}
like image 68
kLabz Avatar answered Nov 19 '25 15:11

kLabz


Starting with Haxe 4.0.0-rc.2, defines with a dot are permitted in #if as long as they are surrounded by parens:

#if (thx.core)

#end

#if thx.core without parens will likely also work in the future.

like image 36
Gama11 Avatar answered Nov 19 '25 13:11

Gama11



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!