Skip to content

Instantly share code, notes, and snippets.

@japgolly
Last active April 26, 2022 02:55
Show Gist options
  • Save japgolly/b145ddf39cc781cfcf97203dd5e30404 to your computer and use it in GitHub Desktop.
Save japgolly/b145ddf39cc781cfcf97203dd5e30404 to your computer and use it in GitHub Desktop.
Scala 2.13 migration notes

scalafix

addSbtPlugin("ch.epfl.scala" % "sbt-scalafix" % "0.9.13")
import scalafix.sbt.ScalafixPlugin.autoImport.{scalafixDependencies, scalafixSemanticdb}

scalafixDependencies in ThisBuild += "org.scala-lang.modules" %% "scala-collection-migrations" % "2.1.4",
libraryDependencies +=  "org.scala-lang.modules" %% "scala-collection-compat" % "2.1.4",
scalacOptions ++= List("-Yrangepos", "-P:semanticdb:synthetics:on"),
scalafixEnable

;test:scalafix Collection213CrossCompat ;scalafix Collection213CrossCompat
;test:scalafix Collection213Upgrade ;scalafix Collection213Upgrade
git add -A && git stash && git stash apply && git co project && git st
git commit -m 'Run Collection213CrossCompat scalafix'

Scala 2.13

for f in $(seq 23); do find . -type f -name '*.scala' -exec perl -pi -e 's/^([^"]+?)→/\1->/g; s/^([^"]+?)←/\1<-/g; s/^([^"]+?)⇒/\1=>/g' {} +; done
find . -type f -name '*.scala' -exec perl -pi -e '
    s/\b(?:Gen)?Traversable/Iterable/g;
    s/\\033/\\u001b/g;
    s/CanBuildFrom *\[[A-Za-z0-9_ \[\]]+, */Factory[/g;
    s/import scala.collection.generic.CanBuildFrom/import scala.collection.Factory/;
    s/\btoIterator\b/iterator/g;
    s/(cbf[A-Z]*)\([a-zA-Z0-9_]*\)/\1.newBuilder/g;
    s/\bto *\[ *([A-Za-z]+) *\]/to(\1)/g;
  ' {} +

utest

find . -type f -path '*/test/*.scala' -exec perl -pi -e '
    s/^( *)'"'"'([^ ]+)\{/\1"\2" - {/;
    s/^( *)'"'"'([^ ]+)( +)(?:- *)?\{/\1"\2"\3- {/;
    s/^( *)'"'"'([^ ]+)( +)-/\1"\2"\3-/;
    s/^(\s+)(?:override +)(?:val|def) +tests += +TestSuite *{ *$/\1override def tests = Tests {/;
  ' {} +

SBT config for macro annotations

  def byScalaVersion[A](f: PartialFunction[(Long, Long), Seq[A]]): Def.Initialize[Seq[A]] =
    Def.setting(CrossVersion.partialVersion(scalaVersion.value).flatMap(f.lift).getOrElse(Nil))

  def addMacroParadisePlugin = Def.settings(
    Seq(
      libraryDependencies ++= byScalaVersion {
        case (2, 12) => Seq(compilerPlugin("org.scalamacros" % "paradise" % Ver.MacroParadise cross CrossVersion.patch))
        case (2, 13) => Nil
      }.value,
      scalacOptions ++= byScalaVersion {
        case (2, 12) => Nil
        case (2, 13) => Seq("-Ymacro-annotations")
      }.value
    ))
@japgolly
Copy link
Author

@ttj4 Yeah, this was how I migrated from 212 to 213

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment