Scala 中的条件编译 [英] Conditional compilation in Scala

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

问题描述

我正在开发一个依赖于 Scala 2.9 但仅用于次要功能的库.我想提出与2.8兼容的版本,但我不想维护两个代码分支.由于我使用的是 SBT,我想从它的交叉编译功能中受益.

I am working on a library which depends on Scala 2.9 but only for a minor feature. I would like to propose version compatible with 2.8, but I don't want to maintain two code branch. Since I'm using SBT, I would like to benefits from it cross-compilation features.

但是我不知道是否有一种方法可以提供等效的条件编译,仅在使用 Scala 2.9 时才包含一段代码.反射性可能是一种选择(但如何选择?).

However I don't know is there is a way to provide an equivalent of conditional compilation, to include a piece of code only if Scala 2.9 is used. Reflexivity could be an option (but how?).

我在 2.9 中使用的功能是新的 sys 包对象.

The features I am using in 2.9 are the new sys package object.

推荐答案

我通过反思得到了它.所以如果我想获得 sys.SystemProperties,我可以这样做:

I got it with reflection. So if I want to get the sys.SystemProperties, I can do:

try {
    val k = java.lang.Class.forName("scala.sys.package$")
    val m = k.getMethod( "props" )
    // etc.
} catch {
    case _ => throw new UnsupportedOperationException("Only available with Scala 2.9")
}

但它是如此无聊和丑陋,我想我会放弃那些功能......

But it is so boring and ugly that I think I will drop those features...

这篇关于Scala 中的条件编译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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