66// example is repeated twice, once with braces as required for Scala 3.0 to 3.2
77// and again with the new syntax that eliminates the braces.
88//
9- // In essense , a `:` (colon) token is also recognized where a function
9+ // In essence , a `:` (colon) token is also recognized where a function
1010// argument would be expected. The examples below are adapted from the Dotty
1111// documentation page:
1212// https://dotty.epfl.ch/docs/reference/other-new-features/indentation.html
@@ -42,9 +42,9 @@ times(3) {
4242// one
4343// two
4444
45- // New braceless syntax to define and pass the function. Now the compiler interprets
46- // the trailing colon, followed by indented lines, as the beginning and definition of
47- // the anonymous function:
45+ // New braceless syntax to define and pass the function. Now the compiler
46+ // interprets the trailing colon, followed by indented lines, as the
47+ // beginning and definition of the anonymous function:
4848times(3 ):
4949 println(" one" )
5050 println(" two" )
@@ -69,8 +69,9 @@ val paths2 = Seq(dir) `++`:
6969val xs = 0 until 10
7070// val xs: Range = Range 0 until 10
7171
72- // What about function arguments? They can either go on the next line after the
73- // colon or on the same line:
72+ // What about function arguments?
73+ // The function arguments can either go on the next line after the colon
74+ // or on the same line:
7475val map1a = xs.map {
7576 x =>
7677 val y = x - 1
@@ -104,7 +105,7 @@ val map3b = xs.map: x
104105
105106// Here are multiple arguments:
106107
107- val fold1 = xs.foldLeft(0 ) { (x, y) =>
108+ val fold1a = xs.foldLeft(0 ) { (x, y) =>
108109 x + y
109110}
110111// val fold1: Int = 45
@@ -120,3 +121,42 @@ val fold2a = xs.foldLeft(0): (x, y) =>
120121// 1 |val fold2b = xs.foldLeft(0): (x, y) => x + y
121122// | ^^^^^^^^^^^^^^^^^^^^^^
122123// | not a legal formal parameter for a function literal
124+
125+ // You'll have to use braces:
126+ val fold1b = xs.foldLeft(0 ) {(x, y) => x + y}
127+ // val fold1b: Int = 45
128+
129+ // So, anonymous functions no longer require braces. What about import statements?
130+ // First, here's what we know works:
131+
132+ import scala .util .{Try , Success , Failure }
133+ import scala .{
134+ Option , Some , None
135+ }
136+
137+ // However, there is no braceless alternative:
138+ // scala> import scala.util: # : instead of .??
139+ // | Try, Success, Failure
140+ // |
141+ // -- Error: ---------------------------------------------------------------------------------------------------------------------------------------------------
142+ // 1 |import scala.util:
143+ // | ^
144+ // | end of statement expected but ':' found
145+
146+ // scala> import scala.util.: # : after .??
147+ // | Try, Success, Failure
148+ // |
149+ // -- [E040] Syntax Error: -------------------------------------------------------------------------------------------------------------------------------------
150+ // 1 |import scala.util.:
151+ // | ^
152+ // | an identifier expected, but ':' found
153+ // |
154+ // | longer explanation available when compiling with `-explain`
155+ // -- [E040] Syntax Error: -------------------------------------------------------------------------------------------------------------------------------------
156+ // 2 | Try, Success, Failure
157+ // | ^
158+ // | '.' expected, but ',' found
159+ // -- [E040] Syntax Error: -------------------------------------------------------------------------------------------------------------------------------------
160+ // 3 |
161+ // |^
162+ // |'.' expected, but eof found
0 commit comments