Reputation: 4685
I want release library for 2.12 and 2.13 scala version. But it depends on another library, which exist for 2.12 only. For 2.13 I wrote my implementation for fast
function:
2.12 code looks:
import com.dongxiguo.fastring.Fastring.Implicits._ //2.12 only
object Lib {
val a = fast"hello world"
}
2.13 code looks:
import mycompat.Implicits._ //2.13 only
object Lib {
val a = fast"hello world" //my implementation
}
So difference - only import ...
in several files.
I can't understund how to organize code for differents scala version.
Upvotes: 5
Views: 1288
Reputation: 1663
Having different imports is problematic, because that means you have different sources (and you need to maintain them). I think providing missing implementation of library in it's own original package will be better solution.
//main/scala-2.13/com/dongxiguo/fastring/Fastring/Implicits.scala
package com.dongxiguo.fastring.Fastring
object Implicits {
//your implementation of fast"Something"
}
As long as it is in scala-2.13
folder it will be compiled and used only for scala-2.13.
You need also different dependencies for 2.12 and 2.13 versions:
libraryDependencies ++= {
CrossVersion.partialVersion(scalaVersion.value) match {
case Some((2, 12)) => Seq("com.dongxiguo" %% "fastring" % "1.0.0")
case Some((2, 13)) => Seq()
case _ => Seq()
}
}
You will have same Lib
implementation without any change for scala 2.13 and when fastring
will be released for new scala version You will just remove those parts.
You can also create your own proxy object that will have distinct implementations for 2.12 and 2.13 in mycompat.Implicits._
.
//main/scala-2.13/mycompat/Implicits.scala
package com.mycompat
object Implicits { /* proxy to fast"Something" from fastring library */ }
//main/scala-2.12/mycompat/Implicits.scala
package com.mycompat
object Implicits { /* your implementation of fast"Something" */ }
This is also good idea.
Upvotes: 6
Reputation: 48410
Based on lihaoyi and What are the Scala version-specific source directories in sbt? try something like so
src/main/scala/example/Hello.scala
:
package example
object Hello extends Greeting with App {
println(greeting)
}
src/main/scala-2.11/example/Greeting.scala
:
package example
trait Greeting {
lazy val greeting: String = "hello-from-2.11.12"
}
src/main/scala-2.13/example/Greeting.scala
:
package example
trait Greeting {
lazy val greeting: String = "hello-from-2.13.1"
}
build.sbt
:
crossScalaVersions := List("2.13.1", "2.11.12")
Now sbt ++2.11.12 run
outputs
hello-from-2.11.12
whilst sbt ++2.13.1 run
outputs
hello-from-2.13.1
Upvotes: 3