现在,我知道Scala中没有预处理器。所以我的问题是:实现一个机制来记录程序活动以进行调试的最好方法是什么,当关闭时至少影响性能?
解决方案您可以使用 scala.annotation.elidable
可以删除调用方法的注释生成的代码。
行为受到将-Xelide-below传递给scalac的影响。如果给予注释的优先级低于命令行参数,则标记为elidable的方法将从生成的代码中省略。示例:
In languages like C/C++/Objective-C it's common to use preprocessor macros to define logging mechanisms that are not even compiled for released binaries, thus causing no performance hit. Something along the lines of:
#ifdef DEBUG printf("some event we want to log\n"); #endifNow, I know there's no preprocessor in Scala. So my question is: what is the best way to implement a mechanism to log program activity for debug purposes, while impacting performance the least when it's turned off?
解决方案You can use scala.annotation.elidable
An annotation for methods for which invocations might be removed in the generated code.
Behavior is influenced by passing -Xelide-below to scalac. Methods marked elidable will be omitted from generated code if the priority given the annotation is lower than to the command line argument. Examples:
更多推荐
调试登录Scala,没有性能影响
发布评论