Conditional scalacOptions with SBT

I am using a cross-build project for Scala 2.8, 2.9 and (hopefully) 2.10 using SBT. I would like to add the -feature when compiling with only 2.10.

In other words, when compiling with a version less than 2.10.0, I would like to set the compiler options as:

 scalacOptions ++= Seq( "-deprecation", "-unchecked" ) 

and when compiling with a version greater than or equal to 2.10.0:

 scalacOptions ++= Seq( "-deprecation", "-unchecked", "-feature" ) 

Is there any way to achieve this?

+5
source share
2 answers

In cross-building, scalaVersion reflects the version your project is currently working with. So depending on the scalaVersion you should do the trick:

 val scalaVersionRegex = "(\\d+)\\.(\\d+).*".r ... scalacOptions <++= scalaVersion { sv => sv match { case scalaVersionRegex(major, minor) if major.toInt > 2 || (major == "2" && minor.toInt >= 10) => Seq( "-deprecation", "-unchecked", "-feature" ) case _ => Seq( "-deprecation", "-unchecked" ) } 
+6
source

I found that it was a quick and concise way to do this:

 scalaVersion := "2.10.0" crossScalaVersions := "2.9.2" :: "2.10.0" :: Nil scalacOptions <<= scalaVersion map { v: String => val default = "-deprecation" :: "-unchecked" :: Nil if (v.startsWith("2.9.")) default else default :+ "-feature" } 
+6
source

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


All Articles