Appendix A. Significant Indentation Versus Braces Syntax
In “New Scala 3 Syntax—Optional Braces”, I introduced the new, optional braces syntax in Scala 3, which is also called significant indentation. This appendix provides concise examples of both forms. The examples are available as scripts in the code examples folder src/script/scala/progscala3/: BracesSyntax.scala and IndentationSyntax.scala. They demonstrate more details than are shown here, such as how to use the optional end markers for significant indentation syntax.
Table A-1 shows examples of each form. Under the “Indentation” column, the for and while loops, and the if expression examples also demonstrate the optional, alternative control syntax. If you don’t pass the flag -new-syntax to the compiler or REPL, you can omit the keywords then and do and use parentheses around the conditions, as shown in the corresponding braces examples.
Also shown are the changes to the import syntax.
For the braces examples, all braces around single expressions can be omitted, but they are shown here to emphasize where they are used in the general case.
| Construct | Indentation | Braces |
|---|---|---|
Package definition |
|
|
Import statements |
|
|
|
|
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access