Skip to Content
Course content

237: Comparing Scala to Kotlin for JVM Development

Click on the "Edit" button in the top corner of the screen to edit your slide content.

Imagine you're choosing between two high-end workshops for a complex project. The first is a Master Builder's shop. Everything is organized for efficiency, the tools are refined versions of classic hammers and saws, and the goal is to get the house built correctly and quickly. The second is an Architect's laboratory. It has 3D printers, parametric design software, and tools that can literally reshape the materials they're working with. The Architect can build things the Master Builder can't even imagine, but they might spend three hours debating the theoretical properties of the foundation before a single nail is driven.

That's the vibe when you're choosing between Kotlin and Scala on the JVM. Kotlin is the Master Builder; it’s designed to be a "better Java," focusing on pragmatism and developer productivity. Scala is the Architect's laboratory; it's a multi-paradigm language that wants to give you the tools to build your own domain-specific languages (DSLs) and push the boundaries of type theory.

Pragmatism vs. Expressive Power

I've spent years in both camps, and the first thing you'll notice is how they handle "missing" data. In Kotlin, null safety is baked into the type system with the `?` operator. It's a brilliant, pragmatic fix for the Billion Dollar Mistake. You have `String` and `String?`, and the compiler forces you to check for nulls.

Scala takes a more theoretical approach. We don't just "mark" a type as nullable; we wrap it in an Option[T]. This turns the absence of a value into a first-class object that you can map over, flatMap, or fold. While Kotlin's approach is faster to write, Scala's approach allows you to chain transformations without ever leaving the "container" of the Option. It's the difference between checking if a door is open before entering (Kotlin) and treating the door itself as a pipeline that might or might not lead somewhere (Scala).

// Kotlin: Pragmatic null check
val name: String? = getName()
val length = name?.length ?: 0

// Scala: Functional transformation
val name: Option[String] = getName()
val length = name.map(_.length).getOrElse(0)

The "Magic" of Implicits and Givens

Here is where the two languages really diverge. Kotlin has extension functions, which let you add methods to existing classes. They're great, they're clean, and they're predictable. But Scala has implicits (and in Scala 3, givens).

Implicits allow the compiler to "fill in the blanks" for you. If a function requires a certain configuration or a type-class instance, Scala will go hunting through your scope to find a matching value and inject it automatically. This is how libraries like Circe or Cats create such incredibly fluid APIs. I'll be honest: it's a double-edged sword. When it works, it feels like magic. When it doesn't, you're staring at a compiler error wondering where on earth that hidden value is coming from. Kotlin avoids this complexity entirely to keep the cognitive load low.

The Compile-Time Tax

We need to talk about the elephant in the room: compile times. Because the Scala compiler is doing so much heavy lifting—resolving implicits, analyzing complex type hierarchies, and optimizing high-level abstractions—it is notoriously slower than Kotlin.

If you're building a mobile app where you need a tight feedback loop, Kotlin is the clear winner. But if you're building a massive data processing engine (like Spark) where the correctness of a complex type transformation is more important than how long it takes to build the JAR, you'll appreciate the rigor Scala brings to the table. I usually tell my juniors: Kotlin is for when you want to get to "done" faster; Scala is for when the problem is so complex that you need a more powerful language to keep your sanity.

Which one actually wins?

It depends on what you're optimizing for. Kotlin is the perfect bridge for Java developers; it removes the boilerplate without breaking the mental model. Scala, however, changes the way you think about programming. It pushes you toward immutability and pure functions not just as a "style," but as a structural necessity. If you love the feeling of "solving the puzzle" at the type level, stick with Scala. If you just want to ship a feature by Friday without fighting the compiler, Kotlin is your friend.




📋 Practical Task

Implementing a Type-Safe Event Dispatcher

To truly feel the difference between the two approaches, you're going to build a small Event Dispatcher. In Scala, we can use Context Bounds and Type Classes to ensure that only events with a defined "Handler" can be dispatched, moving the error from runtime to compile-time.

Your Goal: Create a system where you have different event types (e.g., UserLoggedIn, OrderPlaced) and a Dispatcher. The Dispatcher should have a dispatch method that only accepts an event if there is a corresponding EventHandler[T] available in the implicit scope.

Requirements:

  • Define a trait EventHandler[T] with a method handle(event: T): Unit.
  • Create two case classes for events: UserLoggedIn(userId: String) and OrderPlaced(orderId: Int).
  • Implement a Dispatcher class with a method dispatch[T](event: T)(implicit handler: EventHandler[T]).
  • Provide a given` (Scala 3) or implicit val` (Scala 2) handler for UserLoggedIn, but do not provide one for OrderPlaced.
  • In your main method, attempt to dispatch both events. Verify that the code for UserLoggedIn compiles and runs, while the code for OrderPlaced fails at compile-time.
Rating
0 0

There are no comments for now.

to be the first to leave a comment.