Blocks, indentation, packages and imports

Scala 3 takes the indentation seriously. A block is the lines that are indented under the line that opens it, and the compiler knows where it ends because the indentation ends. The braces of Scala 2 still compile, and so does an explicit end marker, which is the way to close a long block by name.

Indentation is syntax

The program below nests three blocks, one inside a val and one inside an if, and closes the method with an end marker that names it:

blocks.scala

@main def blocks(): Unit = val result = val doubled = val base = 21 base * 2 doubled + 1 println(result) if result > 40 then println("large") println("over the limit") else println("small") println("under the limit") val values = List(1, 2, 3) val sum = values .map(_ * 2) .filter(_ > 2) .sum println(sum) end blocks

scala-cli run --server=false blocks.scala

43
large
over the limit
10

Both the indentation and the line breaks before a dot are the Scala 3 style: a chain that starts with . continues the expression above, and a line with less indentation closes the block. A branch with a single statement is usually written on the same line as then or else, which is why both branches here have two statements. end blocks is optional — the indentation already closed the method — and it is useful exactly when the block is long enough that the reader has lost the opening line.

Braces still work, and there are places where they are the shorter form: a one-line lambda passed to a method, values.filter(x => x > 1) written on one line, and a block in a string or an expression that must stay on one line. The course uses braces only there, and indentation everywhere else.

Packages

A package clause at the top of a file puts every definition in it in that package, and a folder named after the package is the convention a build follows: src/main/scala/shapes/Geometry.scala holds package shapes. A package is a namespace, not a folder check: the compiler accepts a file anywhere as long as the clause matches.

The program below is one file with a package, an object and an entry point, and it runs directly, because scala-cli compiles the file and its package together:

demo.scala

package shapes import shapes.Geometry.area object Geometry: def area(width: Int, height: Int): Int = width * height @main def demo(): Unit = println(area(3, 4))

scala-cli run --server=false demo.scala

12

The import is not required here, because the definition is in the same package. It is there to show the full name: the program could have written shapes.Geometry.area(3, 4) instead.

Imports

An import brings a name into scope, renames it or takes a whole package. The four forms below are the ones the rest of the course uses:

import scala.collection.mutable import scala.collection.mutable.{ArrayBuffer, Map as MutableMap} import scala.util.* import java.time.{LocalDate as Date}

The wildcard is *, not _; the rename is as, not an arrow; and one line may import several names from a package. An import inside a method scopes the name to that method, which is the tidy way to use a mutable collection in one place. An unused import is not an error by default, and -Wunused:all — the setting the quality part uses — turns it into a warning.

That block is a fragment: the page shows the four forms, and a file of four imports alone would compile and do nothing.

Related

Article author: Arthur Isaev