【发布时间】:2017-11-08 05:00:35
【问题描述】:
在 Haxe 中,用点引用定义的正确方法是什么?
比如库thx.core,如何针对库名写条件编译?
#if thx.core
#end
另外,特殊字符有通用规则吗?
【问题讨论】:
标签: haxe conditional-compilation
在 Haxe 中,用点引用定义的正确方法是什么?
比如库thx.core,如何针对库名写条件编译?
#if thx.core
#end
另外,特殊字符有通用规则吗?
【问题讨论】:
标签: haxe conditional-compilation
#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()
宏.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
}
}
【讨论】:
#if "whatever_string" 总是正确的,即使没有-D whatever_string
#if flag 语法正确测试。我用解决方法编辑了我的答案。
Starting with Haxe 4.0.0-rc.2,在#if 中允许使用点定义,只要它们被括号包围:
#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 。
【讨论】: