Scala,Maven和预处理器 [英] Scala, Maven, and preprocessors

查看:203
本文介绍了Scala,Maven和预处理器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道Java中预处理器和宏的所有哲学论据。我不同意,因为有些人可能会滥用语言功能,所以应该排除所有语言功能。

I know all of the philosophical arguments against preprocessors and macros in Java. I don't agree that just because some may abuse a language feature, it should be excluded for all.

我想要包含 __ FILE __ __ LINE __ 我的Java和Scala代码中的宏用于高效日志记录。由于运行时性能影响,任何使用Exception都是不可接受的。那些认为可以在生产代码中关闭日志记录的人应该注意Brian Kernighan的建议:

I would like to include __FILE__ and __LINE__ macros in my Java and Scala code for efficient logging. Any use of Exception is unacceptable because of runtime performance impacts. Those people who argue that logging can be turned off in "production code" should heed the advise of Brian Kernighan:


删除错误消息现在程序正在运行就像在地上戴着降落伞,但是一旦你在空中就把它取下来。

Removing the error messages "now that the program is working" is like wearing a parachute on the ground, but taking it off once you're in the air.

这些宏是否有可能使其成为语言?如果没有,有没有办法使用Maven运行像m4这样的预处理器?

Is there any possibility that these macros might make it into the language? If not, is there any way to run a preprocessor like m4 using Maven?

谢谢。

推荐答案

更新
@ralph
嗯,这是2年多以前的问题,但因为我对<$的需求相同c $ c> __ LINE __ 和 __ FILE __ 你提到了SCALA;这里有一些我正在使用的想法,使用Scala宏(从 v2.10.0-RC1 开始)

Update @ralph Well, this is a question from 2+ years ago, but since I'm having the same need for __LINE__ and __FILE__ and you had mentioned SCALA; here are some ideas I'm having, using Scala macros (as of v2.10.0-RC1)

def $currentPosition:String = macro _currentPosition;
def _currentPosition(c:Context):c.Expr[String]={ import c.universe._;
  val pos = c.enclosingPosition;
  c.Expr(Literal(Constant(
    s"${pos.source.path}: line ${pos.line}, column ${pos.column}" )))
}

编译时评估宏, $ currentPosition 替换为描述其在源代码中的位置的文字字符串。例如,在第13行输入 println ,它会显示:

Macros being evaluated at compile-time, $currentPosition is replaced with a literal string that describes its position in the source code. For example, put in a println at line 13, it displays:

/sandbox/tmp_juno_workspace2/LogMacro_Test/src/test/Trial.scala: line 13, column 15

我没有广泛地使用这些机制,但通过调整这个东西,可以开发他需要的日志记录功能(我应该补充一点,编写宏可能很困难 - 这对我来说!)。

I have not played with these mechanisms extensively, but by tweaking the thing, one can develop the logging features (s)he requires (I should add that writing macros can be difficult - it is for me!).

这篇关于Scala,Maven和预处理器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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