🌟Scala 3 New Features


@Credit - Daniel Ciocîrlan, Kannupriya Kalra, Adrien Piquerez, Scala Center
âš¡ First Impressions: A Clear Roadmap
The course wasted no time. Instead of drowning me in theory, it broke Scala 3 into three digestible categories:
What’s Changed (Syntax, implicits, minor tweaks)
What’s Added (Enums, union types, match types)
What’s Removed (Deprecated syntax, abstract type projections)
No fluff—just a laser-focused guide to what actually matters in Scala 3.
Scala 3 (Dotty) introduces groundbreaking features that enhance expressiveness, performance, and type safety. Having explored opaque types, traits with parameters, enums, and type lambdas, I’ve witnessed firsthand how these innovations simplify complex abstractions while maintaining zero-cost performance.
In this blog, I’ll break down these features with practical examples, comparing them to Scala 2 and highlighting why they matter for modern Scala development.
1. Opaque Types: Zero-Cost Type Safety
The Problem in Scala 2
Previously, creating type-safe wrappers (e.g., UserId
) required runtime overhead:
scala
case class UserId(value: String) // Allocates extra memory
Scala 3’s Solution
Opaque types provide compile-time wrappers with no runtime cost:
scala
object UserDomain:
opaque type UserId = String // Internally a String, externally a unique type
object UserId:
def apply(value: String): UserId = value // Factory method
extension (id: UserId) def value: String = id // Expose API
Key Benefits
✅ Zero runtime overhead (unlike case classes)
✅ Strict encapsulation (prevents accidental misuse)
✅ Domain-driven design (cleaner than type aliases)
Use Case: Ideal for high-performance systems (e.g., financial apps, large-scale data processing).
2. Traits with Constructor Parameters
Scala 2 Limitation
Traits couldn’t take constructor arguments, forcing workarounds with abstract classes:
scala
trait Engine { def power: Int } // Must be implemented by subclass
Scala 3’s Enhancement
Now, traits behave like abstract classes but retain mixin capabilities:
scala
trait Engine(power: Int) // Constructor parameter
class Car extends Engine(300) // Must pass argument
Caveats
âš Diamond inheritance requires uniform constructor args:
scala
trait A(x: Int); trait B extends A; trait C extends A
class D extends B with C(5) // Compiler error (conflicting args)
Use Case: Modular configuration (e.g., dependency injection).
3. First-Class Enums (No More Boilerplate!)
Scala 2’s Verbosity
Enums required manual ADTs:
scala
sealed trait Color
case object Red extends Color
case object Green extends Color
// ...
Scala 3’s Elegance
Native enum
syntax reduces boilerplate:
scala
enum Color:
case Red, Green, Blue // Auto-generates sealed hierarchy
Advanced Features:
Parameterized enums (
case RGB(r: Int, g: Int, b: Int)
)Methods & fields (
def hexCode: String = ...
)Companion objects (for factory methods)
Use Case: State machines, HTTP methods, and other fixed-value domains.
4. Type Lambdas: Taming Higher-Kinded Types
The Problem in Scala 2
Partial type application (e.g., Either[String, *]
) required kind-projector:
scala
// With kind-projector syntax
Functor[Either[String, ?]]
Scala 3’s Native Solution
Type lambdas ([A] =>> F[A]
) enable type-level functions:
scala
type EitherString[A] = [A] =>> Either[String, A] // Partial application
given Monad[EitherString] // Works with type classes
Why It Matters
🔹 Replaces kind-projector (no plugin needed)
🔹 Enables monad instances for Either
(critical for FP libraries)
🔹 Supports higher-kinded abstractions cleanly
Use Case: Generic programming (e.g., Cats, ZIO).
Comparative Analysis: Scala 2 vs Scala 3
Feature | Scala 2 Workaround | Scala 3 Native Support |
Type Safety | Case classes (runtime overhead) | Opaque types (zero-cost) |
Trait Params | Abstract classes | Traits + constructor args |
Enums | Manual ADTs | First-class enum syntax |
Type Lambdas | kind-projector plugin | [A] =>> F[A] native syntax |
Final Thoughts
Scala 3 isn’t just an upgrade—it’s a leap forward in expressiveness and performance. Key takeaways:
Opaque types for zero-cost domain modeling.
Traits with parameters for better modularity.
Enums to eliminate boilerplate.
Type lambdas for advanced FP patterns.
These features make Scala 3 the best choice for building type-safe, high-performance systems.
🎥 Want to Go Deeper?
DO CHECK THIS OUT:
LSUG July 2024 | Anatomy of Scaladex - Kannupriya Kalra
https://www.youtube.com/watch?v=8qWsSiLb08U
🌟 A Special Thank You
Before I wrap up, I need to take a moment to express my gratitude to the incredible people who made this Scala journey possible.
First, to Daniel Ciocîrlan(Rock the JVM) – your course didn’t just teach me Scala; it rewired how I think about code. Thank you for:
Demystifying functional programming with crystal-clear explanations
Making advanced concepts like implicits and monads feel approachable
Your contagious enthusiasm that made learning fun
Most of all, for offering this world-class course for free (watch here)
To my amazing mentors Kannupriya Kalra and Adrien Piquerez – your guidance gave me the courage to tackle Scala head-on. Your patience with my endless questions and your belief in me kept me going when concepts got tough.
And to Scala Center – thank you for fostering such a welcoming community and creating resources that make Scala accessible to all. Your work empowers developers worldwide to embrace this beautiful language.
This wasn’t just about learning syntax – it was about joining a community that values elegant, expressive code. To everyone who helped along the way: You’ve changed how I write software forever.
🎓 CERTIFICATE OF COMPLETION 🎓
✨Scala 3 New Features✨
🔗 Useful Links:
🔸 My GSoC Project: Scaladex Compiler Plugin Support
🔸 GitHub: github.com/vidishagawas121
🔸 LinkedIn: Vidisha Gawas
🔸 Discord: reader_83216
🔸 Previous Post: Before GSoC: My Open Source Journey Begins and Community Bonding Experience at GSoC 2025
🔸Scala Discord Channel: Discord channel link
Subscribe to my newsletter
Read articles from Vidisha Gawas directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
