Scala 3 New Features Cheat Sheet
Covers Scala 3's new syntax: significant indentation, enums, given/using, extension methods, union types, and opaque type aliases.
Optional Braces & Enums
Scala 3 supports significant-indentation syntax and a first-class `enum` construct replacing sealed trait boilerplate.
// significant indentation (braces optional)def greet(name: String): String = if name.isEmpty then "Hello, stranger" else s"Hello, $name"enum Color: case Red, Green, Blueenum Shape: case Circle(radius: Double) case Rectangle(width: Double, height: Double)def area(s: Shape): Double = s match case Shape.Circle(r) => math.Pi * r * r case Shape.Rectangle(w, h) => w * h
`given`/`using` (Replaces `implicit`)
Scala 3 splits implicits into clearer, purpose-specific constructs.
trait Show[T]: def show(t: T): Stringgiven Show[Int] with def show(t: Int): String = t.toStringgiven Show[String] with def show(t: String): String = tdef display[T](value: T)(using s: Show[T]): String = s.show(value)display(42) // uses the Show[Int] givendisplay("hello") // uses the Show[String] given// context bound sugardef display2[T: Show](value: T): String = summon[Show[T]].show(value)
Extension Methods & Union Types
Add methods to existing types without inheritance, and express "one of several types" directly.
extension (s: String) def shout: String = s.toUpperCase + "!""hi".shout // => "HI!"extension [T](xs: List[T]) def secondOption: Option[T] = xs.drop(1).headOption// Union typesdef process(input: Int | String): String = input match case i: Int => s"Number: $i" case s: String => s"Text: $s"process(5) // "Number: 5"process("abc") // "Text: abc"
Opaque Type Aliases
Zero-cost type-safe wrappers with no boxing overhead, replacing value classes for many cases.
opaque type UserId = Longobject UserId: def apply(id: Long): UserId = id extension (id: UserId) def value: Long = idopaque type Meters = Doubleobject Meters: def apply(d: Double): Meters = d extension (m: Meters) def +(other: Meters): Meters = m + other def toDouble: Double = mval u: UserId = UserId(42)// u + 1 // compile error — UserId is not a Long outside its own scope
Scala 2 → Scala 3 Syntax Map
Quick lookup when migrating or reading unfamiliar code.
- implicit val/def (typeclass instance)- now `given ... with` or `given name: Type = ...`
- implicit parameter- now `using` parameter clause
- implicit conversion (def, 1 arg)- now `given Conversion[A, B] with`
- sealed trait + case objects/classes- now often a plain `enum`
- trait + implicit class extension- now `extension (x: T) def foo = ...`
- Either[A, B] as sum boundary- often replaced by native `A | B` union types
- Curly-brace blocks- optional; indentation-based syntax is now first-class
Migrate incrementally with the `-source:3.0-migration` compiler flag (or scalafix rules) rather than hand-rewriting — most Scala 2 implicit-based typeclass code maps mechanically onto given/using and the compiler will point out exactly where it doesn't.
Explore Topics
Professional Web Designing Services
- Responsive Websites
- E-commerce Solutions
- SEO Friendly Design
- Fast & Secure
- Support & Maintenance