Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

scala 3







Introduction to Indentation Syntax in Scala 3

  • Overview: Scala 3 introduces an indentation-based syntax as an alternative to the traditional braces-based syntax, influenced by languages like Python. This change aims to make code more concise and readable.
  • Key Concepts:
    • Significant Whitespace: Whitespace (indentation) is now used to denote block structure, eliminating the need for curly braces in many cases.
    • Compatibility: The indentation-based syntax is optional; Scala 3 codebases can use either or both styles, promoting gradual adoption and compatibility with Scala 2 code.







Basic Rules of Indentation Syntax

  • Indentation Blocks: An indentation block starts after a colon (:) that indicates a control structure (if, while, for), class, or method definition followed by a newline. The subsequent lines must be indented more than the line with the colon.

  • Example - Method Definition:

    def sum(a: Int, b: Int): Int =
      val total = a + b
      total
    
  • Example - Control Structures:

    if a > b then
      println("a is greater")
    else
      println("b is greater or equal")
    
  • Example - Loops:

    for 1 to 10 do
      println("a is greater")
    







Using Indentation for Classes and Objects

  • Classes and Objects: The indentation syntax also applies to class and object definitions, where members are defined within an indentation block.
  • Example:
    class Person(name: String, age: Int):
      def greet = println(s"Hi, my name is $name and I am $age years old.")
    
    object MyApp:
      def main(args: Array[String]): Unit =
        val person = new Person("Alice", 30)
        person.greet
    







Indentation with Match Expressions

  • Match Expressions: Scala 3 enhances match expressions with indentation, making them more readable and expressive.
  • Example:
    val result = x match
      case 1 => "one"
      case 2 => "two"
      case _ => "other"
    







Multiline Expressions and Indentation

  • Multiline Expressions: When writing expressions that span multiple lines, ensure that the continuation lines are indented to signal that the expression is not complete.
  • Example:
    val total = a +
      b +
      c
    







Best Practices

  • Indentation: The indentation is the preferred style
  • Consistency: Stick to one style (either indentation or braces) within a file to maintain readability.
  • Adoption: Start using indentation syntax in smaller, less complex code blocks to become familiar with the concept before applying it to larger codebases.
  • Tool Support: Use Scala 3 compatible tools and IDEs that understand and correctly format indentation-based syntax.







Transitioning from Scala 2 to Scala 3

  • Mixed Syntax: Scala 3 allows mixing both braces and indentation syntax, facilitating gradual migration from Scala 2 to Scala 3.
  • Migration Tools: Leverage Scala 3 migration tools and linters that can automatically convert braces to indentation, aiding in the transition process.