By Alvin Alexander. Last updated: November 26, 2019
Just fooling around a little bit at the moment, here are several ways to write for/do blocks with the “significant indentation” style in Dotty (Scala 3) as of Dotty v20:
import io.Source
def printLines1(source: Source): Unit =
for line <- source.getLines do println(line)
def printLines2(source: Source): Unit =
for line <- source.getLines
do println(line)
def printLines3(source: Source): Unit =
for line <- source.getLines
do
println(line)
def printLines4(source: Source): Unit =
for
line <- source.getLines
do
println(line)
def printLines5(source: Source): Unit =
for
line <- source.getLines
if line.trim != ""
do
// a multiline `do` block
println("in `do` block of `for`")
println(line)
There’s nothing earth-shattering there. I started down this road because I didn’t know what the syntax was for a one-line for/do loop, and after that I became interested in the syntax for the other examples, including the last multiline for/do loop.