使用Haxe宏进行条件编译,而不是#if #end [英] Using Haxe macro for conditional compilation, instead of #if #end

查看:97
本文介绍了使用Haxe宏进行条件编译,而不是#if #end的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们有一个Local类:

Suppose we have a Local class with :

class Local {
    static inline public var logLevel:Int = 3;
}

以及一些功能:

Tool.debug(s:String)   // compiled if logLevel >= 0
Tool.moreinfo(s:String)// compiled if logLevel >= 1
Tool.info(s:String)    // compiled if logLevel >= 2
Tool.trace(s:String)   // compiled if logLevel >= 3
Tool.warn(s:String)    // compiled if logLevel >= 4
Tool.err(s:String)     // compiled if logLevel >= 5

我们可以使用-D和代码中的一些#if来实现.

We can achieve this using -D and some #if in the code.

但是,这意味着一直在修改hxml文件.即使只是一个值,这对我也不理想,因为我的所有配置都位于我的Local类中.

However that implies modifying the hxml file all the time. Even if it's just for one value, this isn't ideal for me as all my configuration sits in my Local class.

如果我们使用简单的if()测试值,那么即使没有使用过,代码也会包含所有if和字符串,这会变得更大(因为logLevel表示为常量").

And if we test the value with simple if (), the code gets bigger with all the if and strings, even if it's never used (because the logLevel is meant as a 'constant').

是否可以使用宏来克服这两个问题?

Is it possible to use macros to overcome those two problems?

推荐答案

您可以使记录功能内联.编译器将内联它们并在编译时检查条件,删除没有机会执行的分支.

You can just make your logging functions inline. Compiler will inline them and check the conditions in compile time, removing branches which wouldn't have a chance to be executed.

这篇关于使用Haxe宏进行条件编译,而不是#if #end的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆