Conditional Compilation in Scala

I am working on a library that depends on Scala 2.9, but only for a minor function. I would like to offer a version compatible with 2.8, but I do not want to support two branches of code. Since I use SBT, I would like to take advantage of the cross compilation features.

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

Edit: The functions that I use in 2.9 are the new sys package object.

+6
source share
2 answers

I got it with a reflection. Therefore, if I want to get 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 these functions ...

+2
source

Source: https://habr.com/ru/post/892005/


All Articles