Scala 中的条件编译

编程入门 行业动态 更新时间:2024-10-27 14:26: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 中的条件编译

本文发布于:2023-10-05 10:23:58,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1467354.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:条件   Scala

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!