Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to organize code for multiple scala versions and differents dependencies

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.

like image 825
zella Avatar asked Mar 18 '20 12:03

zella


1 Answers

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.

like image 70
Scalway Avatar answered Oct 02 '22 13:10

Scalway