Skip to Content
Programming Scala, 3rd Edition
book

Programming Scala, 3rd Edition

by Dean Wampler
May 2021
Intermediate to advanced
553 pages
13h 45m
English
O'Reilly Media, Inc.
Content preview from Programming Scala, 3rd Edition

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.

Table A-1. Significant indentation versus braces and other syntax changes
Construct Indentation Braces

Package definition

package mypkg:
  // ...
package mypkg {
  // ...
}

Import statements

import foo.bar.{given, *}
import foo.X as FooX
import baz.{A as _, *}
import foo.bar._
import foo.{X => FooX}
import baz.{A => _, _}

for comprehension

val evens = for
  i <- 0 until 10
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

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Programming Scala, 2nd Edition

Programming Scala, 2nd Edition

Dean Wampler, Alex Payne
Functional Programming in Scala, Second Edition

Functional Programming in Scala, Second Edition

Runar Bjarnason, Michael Pilquist, Paul Chiusano

Publisher Resources

ISBN: 9781492077886Errata Page