Scala 3 Features
Part 1 - Clint Combs
Dotty
Scala 3 Feature Groups - 68 Changes
Scala 3 Roadmap Update
December 3, 2008
December 3, 2008�Python 3 Released
Python 3 All Over Again? No
“Our strategy for making your upgrade predictable and manageable comprises:
Migration - Scala 2.12/2.13 to Scala 3.0
TASTy
Features...
Extension Methods
def (c: Coach) age: Int = Period.between(c.birthDate, LocalDate.now).getYears
val age = coach.age
// extension methods and operators
case class Color(rgb: Int)
def (c: Color) & (other: Color): Color = Color(c.rgb & other.rgb)
Enumerations
enum Position(val numeric: Int) {
case Pitcher extends Position(1)
case Catcher extends Position(2)
case FirstBase extends Position(3)
case SecondBase extends Position(4)
case ThirdBase extends Position(5)
case Shortstop extends Position(6)
case LeftField extends Position(7)
case CenterField extends Position(8)
case RightField extends Position(9)
}
Algebraic Data Types
enum Color(val rgb: Int) {
case Red extends Color(0xFF0000)
case Green extends Color(0x00FF00)
case Blue extends Color(0x0000FF)
case Mix(mix: Int) extends Color(mix)
}
Intersection Types
trait Employee {
def firstName: String
def lastName: String
def age: Int
}
trait SoftballPlayer { def position: Position }
case class Player(firstName: String, lastName: String, age: Int, position: Position)
extends Employee with SoftballPlayer
val p: Employee & SoftballPlayer = Player("John", "Doe", 30, Pitcher)
Union Types
val team = List[Player](
Player("John", "Doe", 30, Pitcher), Player("Catcher", "Doe", 31, Catcher))
val coach = Coach("Earl", "Monroe", LocalDate.of(1967, 3, 10))
val entireTeam: List[Player | Coach] = team ++ List(coach)
def [A <: Player | Coach](team: List[A]) averagePlayerCoachAge: Double = {
team.map {
case Player(_, _, age, _) => age
case c: Coach => c.age
}.sum.toDouble / team.size.toDouble }
Explicit Nulls
Scala Type Hierarchy
Scala Type Hierarchy with Explicit Nulls
Explicit Null Type Syntax
val s1: String = “test”�val s2: String = null // type error!�val s3: String | Null = “test”�val s4: String | Null = null��// type alias�type StringOrNull = String | Null
Explicit Nulls: Calling Existing Code
Coming Soon...
Scala 3 Features - Part 2