12
votes

When building with Scala 2.10 and SBT 0.13.2, I want to have -language:_, but this isn't recognized by Scala 2.9. There's a question about settings different scalacOptions for cross-compilation (Conditional scalacOptions with SBT), but it is about build.sbt. I'm using Build.scala because I'm doing a multi-project build.

I have tried this:

  def scalacOptionsVersion(v: String) = {
    Seq(
      "-unchecked",
      "-deprecation",
      "-Xlint",
      "-Xfatal-warnings",
      "-Ywarn-dead-code",
      "-target:jvm-1.7",
      "-encoding", "UTF-8") ++ (
    if (v.startsWith("2.9")) Seq() else Seq("-language:_"))
  }

  override val settings = super.settings ++ Seq(
    ...,
    scalaVersion := "2.10.4",
    scalacOptions <++= scalaVersion(scalacOptionsVersion),
    crossScalaVersions := Seq("2.9.2", "2.10.4", "2.11.4"),
    ...
  )

but I get an error:

[error] /Users/benwing/devel/lemkit/scala/project/build.scala:29: type mismatch;
[error]  found   : sbt.Def.Initialize[Equals]
[error]  required: sbt.Def.Initialize[sbt.Task[?]]
[error] Note: Equals >: sbt.Task[?], but trait Initialize is invariant in type T.
[error] You may wish to define T as -T instead. (SLS 4.5)
[error]     scalacOptions <++= scalaVersion(scalacOptionsVersion),
[error]                                    ^
[error] one error found

Help?

1
What version of sbt ? - sksamuel
My project is designed for 0.13.2. Maybe there's a way of doing multi-project builds using build.sbt but I don't know how and it seems the answer shouldn't require switching your whole build script. - Urban Vagabond
Just included SBT version in question. - Urban Vagabond

1 Answers

13
votes

In SBT 0.13+ this will work:

def scalacOptionsVersion(scalaVersion: String) = {
  Seq(
    "-unchecked",
    "-deprecation",
    "-Xlint",
    "-Xfatal-warnings",
    "-Ywarn-dead-code",
    "-target:jvm-1.7",
    "-encoding", "UTF-8"
  ) ++ CrossVersion.partialVersion(scalaVersion) match {
         case Some((2, scalaMajor)) if scalaMajor == 9 => Nil
         case _ => Seq("-language:_")
       }
}


val appSettings = Seq(
  scalacOptions := scalacOptionsVersion(scalaVersion.value)

  // other settings...
)