Significant Indentation
Optional braces.
Optional Braces
Scala 3 introduces significant indentation, also called the optional braces syntax. Instead of wrapping blocks in { }, you can use indentation to delimit them.
- Curly braces still work and are fully valid.
- Indentation-based code is cleaner and Python-like.
object Main:
def main(args: Array[String]): Unit =
println("No braces needed")Indented Method Bodies
A method body that follows = can be an indented block. Every statement at the same indentation level belongs to that body.
The last expression is the return value.
object Main:
def square(x: Int): Int =
val result = x * x
result
def main(args: Array[String]): Unit =
println(square(5))All lessons in this course
- Significant Indentation
- Enums in Scala 3
- Opaque Types
- Union and Intersection Types