Scala 2.12 → 2.13: The "Bridge" Upgrade
The jump from 2.12 to 2.13 is often harder than the jump from 2.13 to 3.3. This is because 2.13 completely rewrote the Scala Collections library. This lesson covers how to navigate these breaking changes without losing your mind.
Upgrading sbt and Dependencies
Before touching any Scala code, you must upgrade your build tool and libraries.
Real-World Example: Dependency Eviction
In a large project, you might find that library-A requires Scala 2.13 but library-B hasn't been updated yet. In Scala 2, you cannot mix versions.
The Fix: Use the sbt command evicted to find incompatibilities. If a library is dead (no 2.13 release), you may need to find an alternative or fork it.
The Collections Overhaul
The biggest change in 2.13 is how collections (List, Seq, Map, etc.) are structured.
1. Seq is now immutable.Seq
In 2.12, Seq was an alias for scala.collection.Seq. In 2.13, it's scala.collection.immutable.Seq.
If you were passing a mutable ArrayBuffer where a Seq was expected, your code will now fail to compile.
2. The .to method change
In 2.12, you converted collections like this: list.to[Set].
In 2.13, the syntax changed to: list.to(Set).
Real-World Tip: Don't fix these manually. Use ScalaFix.
sbt "scalafix DeclareContext" // Example command
Handling SAM (Single Abstract Method) Conversions
Scala 2.13 tightened how it handles conversions to Java interfaces.
- 2.12: Often worked "by magic" or with obscure compiler flags.
- 2.13: Much stricter. You might need to explicitly define the conversion or use the
-Xsource:3flag to get Scala 3-like behavior early.
Keeping the Codebase Shippable
You cannot stop development for a month to migrate. Use Shadow Builds.
Strategy: The Parallel CI
- Keep your main build on 2.12.
- Create a
build.sbtconfiguration (or use a environment variable) to toggle the Scala version. - Fix errors in small batches. Use
scala-collection-compatlibrary to write code that compiles on both 2.12 and 2.13.
// Add this to your dependencies to bridge the gap
libraryDependencies += "org.scala-lang.modules" %% "scala-collection-compat" % "2.11.0"
Summary
The goal of the 2.13 upgrade is to reach a state where your code compiles with the new collections library. Once you are on 2.13, you have unlocked the door to Scala 3.
Comments
Be the first to comment on this lesson!