6
votes

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.

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?).

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

2

2 Answers

2
votes

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...