Scala
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Object-Oriented Scala
-
Section 4: Functional Scala
-
Section 5: Collections in Depth
-
Section 6: Type System
-
Section 7: Concurrency and Ecosystem
-
Section 8: Practical Projects
-
Section 9: Interview Practice
-
Section 10: Data Structures and Algorithms in Scala
-
Section 11: More Practice Exercises
-
Section 12: Advanced Functional Patterns
-
Section 13: More Ecosystem
-
Section 14: Scala Collections Library Deep Dive
-
Section 15: Scala Standard Library Deep Dive
-
Section 16: Akka Ecosystem Deep Dive
-
Section 17: Cats and Cats Effect Deep Dive
-
Section 18: Play Framework Deep Dive
-
Section 19: Apache Spark with Scala Deep Dive
-
Section 20: Scala Build Tools Deep Dive
-
Section 21: Scala 3 Specific Features
-
90: Union and Intersection Types
-
Section 22: Scala Testing Deep Dive
-
Section 23: Functional Domain Modeling
-
Section 24: More Data Structures and Algorithms in Scala
-
Section 25: Scala for Data Engineering
-
Section 26: More Practical Projects
-
Section 27: More Interview and Review
-
Section 28: ZIO Ecosystem Deep Dive
-
Section 29: Scala for Machine Learning
-
Section 30: Scala Microservices Architecture
-
Section 31: Scala Type System Deep Dive
-
Section 32: More Practice and Drills
-
Section 33: Scala Performance Deep Dive
-
Section 34: Scala Ecosystem Tooling
-
Section 35: Scala for Reactive Systems
-
Section 36: More Real-World Case Studies
-
Section 37: Scala for Financial Systems
-
Section 38: Scala GraphQL and gRPC
-
Section 39: More Final Projects
-
Section 40: More Interview and Final Review
-
Section 41: Scala for Streaming Data
-
Section 42: Scala Security Practices
-
Section 43: More Language Deep Dive
-
Section 44: Scala Command-Line Tools
-
Section 45: Scala Documentation and Style
-
Section 46: Scala Dependency Management
-
Section 47: More Practical Backend Patterns
-
Section 48: Scala for Event-Driven Architecture
-
Section 49: More Practice Drills Round 2
-
Section 50: Scala Compiler Deep Dive
-
Section 51: Scala for Web Frontends
-
Section 52: More Data Engineering Practice
-
Section 53: Scala Observability
-
Section 54: More Advanced Practice Projects
-
Section 55: Scala for Legacy Java Integration
-
Section 56: More Testing Practice
-
Section 57: Final Mastery Review
-
Section 58: Scala History and Ecosystem Context
-
Section 59: More Concurrency Patterns
-
Section 60: Scala for Configuration Management
-
Section 61: More Domain Modeling Practice
86: Cross-Building for Multiple Scala Versions
I've been there. You've spent a weekend writing a sleek little utility library, you've tested it on your machine (which is running Scala 2.13), and you push it to your team. Then, ten minutes later, you get a Slack message: "Why is this throwing a NoSuchMethodError on my 2.12 project?"
The problem is that Scala is not binary compatible across major versions. A JAR compiled for 2.13 simply won't work for someone on 2.12, and certainly not for someone on Scala 3. If you're writing a library meant for others to use, you can't just pick one version and hope for the best.
The "Single Version" Trap
Here is a typical build.sbt for a library that's about to cause a lot of friction:
name := "scala-utils"
organization := "com.example"
scalaVersion := "2.13.12"
libraryDependencies += "org.typelevel" %% "cats-core" % "2.9.0"
At first glance, this looks fine. But notice that scalaVersion is hardcoded. When you run sbt publish, sbt produces a single artifact: scala-utils_2.13-1.0.jar. When your teammate tries to add this to their 2.12 project, the build will either fail to find the dependency or, if they force it, the JVM will crash at runtime because the bytecode doesn't match the expected runtime version.
You might think, "I'll just manually change the version string and publish again," but that's a recipe for disaster. You'll forget a version, or worse, you'll realize your code uses a feature from 2.13 (like LazyList) that doesn't even exist in 2.12, and your manual process will miss the compilation error until it's too late.
Automating the Matrix with crossScalaVersions
The professional way to handle this is to tell sbt that your project is intended to be "cross-built." We do this by defining a list of supported versions and using the + prefix for our commands.
Update your build.sbt like this:
name := "scala-utils"
organization := "com.example"
// This is your primary development version
scalaVersion := "2.13.12"
// These are the versions you want to support
crossScalaVersions := Seq("2.12.18", "2.13.12", "3.3.1")
libraryDependencies += "org.typelevel" %% "cats-core" % "2.9.0"
Notice that %% in the dependency? That's the "magic" operator. It tells sbt to append the current Scala binary version to the artifact name. If sbt is building for 2.12, it looks for cats-core_2.12; if it's 3.3, it looks for cats-core_3.
Now, instead of running sbt publish, you run:
sbt +publish
That little + is a signal to sbt: "Run this command for every version listed in crossScalaVersions." sbt will now compile, test, and publish three separate JARs, ensuring that every single one is binary compatible with its target version.
Handling Version-Specific Code
Now, here is the real challenge. What if you want to use LazyList (introduced in 2.13) but you still have to support 2.12, where only Stream exists? You can't put both in the same file without the 2.12 compiler screaming at you.
sbt has a built-in convention for this. You can create version-specific source folders. Your directory structure should look like this:
src/main/scala/(Code that works on ALL versions)src/main/scala-2.12/(Code only for 2.12)src/main/scala-2.13/(Code only for 2.13)src/main/scala-3/(Code only for Scala 3)
I usually define a common trait in the main scala/ folder and then provide version-specific implementations in the versioned folders. For example, you might define a CollectionUtils trait in src/main/scala, and then implement the specific "lazy sequence" logic in src/main/scala-2.12 using Stream and in src/main/scala-2.13 using LazyList. When sbt cross-builds, it only includes the folder that matches the current target version.
It's a bit more boilerplate, but it's the only way to maintain a high-quality library that doesn't break your users' builds.
📋 Practical Task
Exercise: Build a Cross-Version Compatibility Bridge
You are tasked with creating a small library called compat-bridge that must support both Scala 2.13 and Scala 3. The library needs to provide a method that returns a "version identity" string.
Requirements:
- Configure
build.sbtto supportcrossScalaVersionsfor2.13.12and3.3.1. - Create a trait
VersionInfoinsrc/main/scala/VersionInfo.scalawith a methoddef versionString: String. - Create an object
VersionImplinsrc/main/scala-2.13/VersionImpl.scalathat extendsVersionInfoand returns"Built for Scala 2.13". - Create an object
VersionImplinsrc/main/scala-3/VersionImpl.scalathat extendsVersionInfoand returns"Built for Scala 3". - Run
sbt +compileand verify that sbt compiles the project successfully for both versions.
Success Criteria: Your project should compile without errors when running the +compile command, proving that sbt is correctly swapping the source folders based on the target Scala version.
There are no comments for now.