Scala Advanced
Scala Advanced is the expert level of Scala 3. It builds on the Beginner and Intermediate courses with deeper tools for large programs.
A case class groups named values
A case class groups values under names. The program prints the title and the lesson count of one course.
case class Course(title: String, lessons: Int)
@main def main(): Unit =
val course = Course("Scala Advanced", 45)
println(course.title)
println(course.lessons)A list can be transformed and summed
The map step doubles each number. The sum step adds the doubled numbers.
@main def main(): Unit =
val numbers = List(1, 2, 3)
println(numbers.map(n => n * 2).sum)An Option match handles a missing value
The match doubles a present number. It gives zero when no number is present.
@main def main(): Unit =
val maybe: Option[Int] = Some(5)
val doubled = maybe match
case Some(n) => n * 2
case None => 0
println(doubled)Example
case class Lesson(title: String, minutes: Int)
@main def main(): Unit =
val lessons = List(Lesson("Types", 5), Lesson("Patterns", 4), Lesson("Projects", 6))
println(lessons.map(l => l.minutes).sum)
println(lessons.length)Key points
- Advanced types: Advanced types let a program state stricter rules in its type system.
- Givens depth: Givens depth covers how given values are found and chosen.
- Metaprogramming: Metaprogramming is code that writes or checks other code.
- Concurrency: Concurrency is work that runs in overlapping parts.
- Patterns: Patterns are reusable shapes for solving common problems.
- Performance: Performance work measures a program and removes its slow parts.
- Projects: Two projects close the course by joining the new tools in real programs.
In this example, the program builds a list of three lessons. It sums their minutes to get 15. It counts the list to get 3.
Try it Yourself
Edit the code, then press Run.
Running is simulated Β· Scastie
Exercise
What does Scala Advanced add on top of the Intermediate course?