在Haxe中,使用点引用定义的正确方法是什么?
例如,对于库thx.core
,如何针对库名写入条件编译?
#if thx.core
#end
此外,是否有特殊字符的一般规则?
#if flag
语法似乎不处理点。但是,Compiler.getDefine()
处理大多数字符,包括点:
hxml / build命令:-D é'"(-è_.çà)=test
#if "é'\"(-è_çà)"
trace('will always be called, even without the -D');
#end
trace(haxe.macro.Compiler.getDefine("é'\"(-è_.çà)")); // test
有一个带有初始化宏的点的解决方法,即使它不是很漂亮:
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
}
}
Starting with Haxe 4.0.0-rc.2,在#if
中允许使用点定义,只要它们被parens包围:
#if (thx.core)
#end
#if thx.core
无父will likely also work in the future。
在thx.core
的情况下,您可以使用thx_core
(带下划线):
#if thx_core
#end
据我所知,对连字符以外的特殊字符(those get translated into underscores by the compiler)没有普遍的支持。
thx.core
库defines -D thx_core
本身,使用haxelib的support for extraParams.hxml
。