Chapter 2. Type Less, Do More
This chapter continues our tour of Scala features that promote succinct, flexible code. We’ll discuss organization of files and packages, importing other types, variable and method declarations, a few particularly useful types, and miscellaneous syntax conventions.
New Scala 3 Syntax—Optional Braces
If you have prior Scala experience, Scala 3 introduces a new optional braces syntax that makes it look a lot more like Python or Haskell, where curly braces, {
…}
, are replaced with significant indentation. The examples in the previous chapter and throughout the book use it.
This syntax is more concise and easier to read. It will also appeal to Python developers who learn Scala because it will feel a little more familiar to them (and vice versa). When data scientists who use Python and data engineers who use Scala work together, this can help them collaborate. Also, since many new programmers learn Python as a first language, learning Scala will be that much easier.
There is also a new syntax for control structures like for
loops and if
expressions. For example, there is if condition then
… instead of the older if (condition)
…. Also, there is for
…do println(
…)
instead of for {
…} println(
…)
.
The disadvantage of these changes is that they are strictly not necessary. Some breaking changes in Scala 3 are necessary to move the language forward, but you could argue these syntax changes aren’t essential. You also have to be careful to use spaces or tabs consistently ...
Get Programming Scala, 3rd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.