Skip to content

Commit 9359dfd

Browse files
committed
Indent after match
1 parent 6ad8502 commit 9359dfd

19 files changed

+209
-207
lines changed

docs/docs/reference/changed-features/match-syntax.md

+18-8
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,31 @@ The syntactical precedence of match expressions has been changed.
1010

1111
```scala
1212
xs match {
13-
case Nil => "empty"
14-
case x :: xs1 => "nonempty"
13+
case Nil => "empty"
14+
case x :: xs1 => "nonempty"
1515
} match {
16-
case "empty" => 0
17-
case "nonempty" => 1
16+
case "empty" => 0
17+
case "nonempty" => 1
1818
}
1919
```
2020

21+
(or, dropping the optional braces)
22+
23+
```scala
24+
xs match
25+
case Nil => "empty"
26+
case x :: xs1 => "nonempty"
27+
match
28+
case "empty" => 0
29+
case "nonempty" => 1
30+
```
31+
2132
2. `match` may follow a period:
2233

2334
```scala
24-
if xs.match {
25-
case Nil => false
26-
case _ => true
27-
}
35+
if xs.match
36+
case Nil => false
37+
case _ => true
2838
then "nonempty"
2939
else "empty"
3040
```

docs/docs/reference/changed-features/numeric-literals.md

+26-26
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ val y: BigInt = 0x123_abc_789_def_345_678_901
1515
val z: BigDecimal = 110_222_799_799.99
1616

1717
(y: BigInt) match
18-
case 123_456_789_012_345_678_901 =>
18+
case 123_456_789_012_345_678_901 =>
1919
```
2020
The syntax of numeric literals is the same as before, except there are no pre-set limits
2121
how large they can be.
@@ -133,27 +133,27 @@ object BigFloat:
133133
def apply(digits: String): BigFloat =
134134
val (mantissaDigits, givenExponent) =
135135
digits.toUpperCase.split('E') match
136-
case Array(mantissaDigits, edigits) =>
137-
val expo =
138-
try FromDigits.intFromDigits(edigits)
139-
catch case ex: FromDigits.NumberTooLarge =>
140-
throw FromDigits.NumberTooLarge(s"exponent too large: $edigits")
141-
(mantissaDigits, expo)
142-
case Array(mantissaDigits) =>
143-
(mantissaDigits, 0)
136+
case Array(mantissaDigits, edigits) =>
137+
val expo =
138+
try FromDigits.intFromDigits(edigits)
139+
catch case ex: FromDigits.NumberTooLarge =>
140+
throw FromDigits.NumberTooLarge(s"exponent too large: $edigits")
141+
(mantissaDigits, expo)
142+
case Array(mantissaDigits) =>
143+
(mantissaDigits, 0)
144144
val (intPart, exponent) =
145145
mantissaDigits.split('.') match
146-
case Array(intPart, decimalPart) =>
147-
(intPart ++ decimalPart, givenExponent - decimalPart.length)
148-
case Array(intPart) =>
149-
(intPart, givenExponent)
146+
case Array(intPart, decimalPart) =>
147+
(intPart ++ decimalPart, givenExponent - decimalPart.length)
148+
case Array(intPart) =>
149+
(intPart, givenExponent)
150150
BigFloat(BigInt(intPart), exponent)
151151
```
152152
To accept `BigFloat` literals, all that's needed in addition is a `given` instance of type
153153
`FromDigits.Floating[BigFloat]`:
154154
```scala
155-
given FromDigits: FromDigits.Floating[BigFloat] with
156-
def fromDigits(digits: String) = apply(digits)
155+
given FromDigits: FromDigits.Floating[BigFloat] with
156+
def fromDigits(digits: String) = apply(digits)
157157
end BigFloat
158158
```
159159
Note that the `apply` method does not check the format of the `digits` argument. It is
@@ -194,17 +194,17 @@ no code that can be executed at runtime. That is why we define an intermediary c
194194
method in the `FromDigits` given instance. That method is defined in terms of a macro
195195
implementation method `fromDigitsImpl`. Here is its definition:
196196
```scala
197-
private def fromDigitsImpl(digits: Expr[String])(using ctx: Quotes): Expr[BigFloat] =
198-
digits.value match
199-
case Some(ds) =>
200-
try
201-
val BigFloat(m, e) = apply(ds)
202-
'{BigFloat(${Expr(m)}, ${Expr(e)})}
203-
catch case ex: FromDigits.FromDigitsException =>
204-
ctx.error(ex.getMessage)
205-
'{BigFloat(0, 0)}
206-
case None =>
207-
'{apply($digits)}
197+
private def fromDigitsImpl(digits: Expr[String])(using ctx: Quotes): Expr[BigFloat] =
198+
digits.value match
199+
case Some(ds) =>
200+
try
201+
val BigFloat(m, e) = apply(ds)
202+
'{BigFloat(${Expr(m)}, ${Expr(e)})}
203+
catch case ex: FromDigits.FromDigitsException =>
204+
ctx.error(ex.getMessage)
205+
'{BigFloat(0, 0)}
206+
case None =>
207+
'{apply($digits)}
208208
end BigFloat
209209
```
210210
The macro implementation takes an argument of type `Expr[String]` and yields

docs/docs/reference/changed-features/operators.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ val x: String or Int = ...
6060

6161
### Motivation
6262

63-
The purpose of the `infix` modifier is to achieve consistency across a code base in how a method or type is applied. The idea is that the author of a method decides whether that method should be applied as an infix operator or in a regular application. Use sites then implement that decision consistently.
63+
The purpose of the `infix` modifier is to achieve consistency actross a code base in how a method or type is applied. The idea is that the author of a method decides whether that method should be applied as an infix operator or in a regular application. Use sites then implement that decision consistently.
6464

6565
### Details
6666

docs/docs/reference/changed-features/pattern-matching.md

+18-18
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,9 @@ object Even:
104104
def unapply(s: String): Boolean = s.size % 2 == 0
105105

106106
"even" match
107-
case s @ Even() => println(s"$s has an even number of characters")
108-
case s => println(s"$s has an odd number of characters")
109-
107+
case s @ Even() => println(s"$s has an even number of characters")
108+
case s => println(s"$s has an odd number of characters")
109+
110110
// even has an even number of characters
111111
```
112112

@@ -134,8 +134,8 @@ object FirstChars:
134134
def unapply(s: String): FirstChars = new FirstChars(s)
135135

136136
"Hi!" match
137-
case FirstChars(char1, char2) =>
138-
println(s"First: $char1; Second: $char2")
137+
case FirstChars(char1, char2) =>
138+
println(s"First: $char1; Second: $char2")
139139

140140
// First: H; Second: i
141141
```
@@ -155,8 +155,8 @@ object Nat:
155155
def unapply(x: Int): Nat = new Nat(x)
156156

157157
5 match
158-
case Nat(n) => println(s"$n is a natural number")
159-
case _ => ()
158+
case Nat(n) => println(s"$n is a natural number")
159+
case _ => ()
160160

161161
// 5 is a natural number
162162
```
@@ -175,8 +175,8 @@ object ProdEmpty:
175175
def get = this
176176

177177
"" match
178-
case ProdEmpty(_, _) => ???
179-
case _ => ()
178+
case ProdEmpty(_, _) => ???
179+
case _ => ()
180180
```
181181

182182

@@ -186,10 +186,10 @@ case _ => ()
186186

187187
```Scala
188188
type X = {
189-
def lengthCompare(len: Int): Int // or, `def length: Int`
190-
def apply(i: Int): T1
191-
def drop(n: Int): scala.Seq[T2]
192-
def toSeq: scala.Seq[T3]
189+
def lengthCompare(len: Int): Int // or, `def length: Int`
190+
def apply(i: Int): T1
191+
def drop(n: Int): scala.Seq[T2]
192+
def toSeq: scala.Seq[T3]
193193
}
194194
```
195195

@@ -200,13 +200,13 @@ type X = {
200200

201201
```scala
202202
object CharList:
203-
def unapplySeq(s: String): Option[Seq[Char]] = Some(s.toList)
203+
def unapplySeq(s: String): Option[Seq[Char]] = Some(s.toList)
204204

205205
"example" match
206-
case CharList(c1, c2, c3, c4, _, _, _) =>
207-
println(s"$c1,$c2,$c3,$c4")
208-
case _ =>
209-
println("Expected *exactly* 7 characters!")
206+
case CharList(c1, c2, c3, c4, _, _, _) =>
207+
println(s"$c1,$c2,$c3,$c4")
208+
case _ =>
209+
println("Expected *exactly* 7 characters!")
210210

211211
// e,x,a,m
212212
```

docs/docs/reference/changed-features/vararg-patterns.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ like one writes them in expressions, using a `: _*` type annotation:
88

99
```scala
1010
xs match
11-
case List(1, 2, xs: _*) => println(xs) // binds xs
12-
case List(1, _ : _*) => // wildcard pattern
11+
case List(1, 2, xs: _*) => println(xs) // binds xs
12+
case List(1, _ : _*) => // wildcard pattern
1313
```
1414

1515
The old syntax, which is shorter but less regular, is no longer supported.

docs/docs/reference/contextual/derivation-macro.md

+47-47
Original file line numberDiff line numberDiff line change
@@ -46,17 +46,17 @@ given derived[T: Type](using Quotes): Expr[Eq[T]] =
4646
val ev: Expr[Mirror.Of[T]] = Expr.summon[Mirror.Of[T]].get
4747

4848
ev match
49-
case '{ $m: Mirror.ProductOf[T] { type MirroredElemTypes = elementTypes }} =>
50-
val elemInstances = summonAll[elementTypes]
51-
val eqProductBody: (Expr[T], Expr[T]) => Expr[Boolean] = (x, y) =>
52-
elemInstances.zipWithIndex.foldLeft(Expr(true: Boolean)) {
53-
case (acc, (elem, index)) =>
54-
val e1 = '{$x.asInstanceOf[Product].productElement(${Expr(index)})}
55-
val e2 = '{$y.asInstanceOf[Product].productElement(${Expr(index)})}
56-
'{ $acc && $elem.asInstanceOf[Eq[Any]].eqv($e1, $e2) }
57-
}
58-
59-
'{ eqProduct((x: T, y: T) => ${eqProductBody('x, 'y)}) }
49+
case '{ $m: Mirror.ProductOf[T] { type MirroredElemTypes = elementTypes }} =>
50+
val elemInstances = summonAll[elementTypes]
51+
val eqProductBody: (Expr[T], Expr[T]) => Expr[Boolean] = (x, y) =>
52+
elemInstances.zipWithIndex.foldLeft(Expr(true: Boolean)) {
53+
case (acc, (elem, index)) =>
54+
val e1 = '{$x.asInstanceOf[Product].productElement(${Expr(index)})}
55+
val e2 = '{$y.asInstanceOf[Product].productElement(${Expr(index)})}
56+
'{ $acc && $elem.asInstanceOf[Eq[Any]].eqv($e1, $e2) }
57+
}
58+
59+
'{ eqProduct((x: T, y: T) => ${eqProductBody('x, 'y)}) }
6060

6161
// case for Mirror.ProductOf[T]
6262
// ...
@@ -78,19 +78,19 @@ Instead we extract the tuple-type for element types using pattern matching over
7878
quotes and more specifically of the refined type:
7979

8080
```scala
81-
case '{ $m: Mirror.ProductOf[T] { type MirroredElemTypes = elementTypes }} => ...
81+
case '{ $m: Mirror.ProductOf[T] { type MirroredElemTypes = elementTypes }} => ...
8282
```
8383

8484
Shown below is the implementation of `summonAll` as a macro. We assume that
8585
given instances for our primitive types exist.
8686

8787
```scala
88-
def summonAll[T: Type](using Quotes): List[Expr[Eq[_]]] =
89-
Type.of[T] match
90-
case '[String *: tpes] => '{ summon[Eq[String]] } :: summonAll[tpes]
91-
case '[Int *: tpes] => '{ summon[Eq[Int]] } :: summonAll[tpes]
92-
case '[tpe *: tpes] => derived[tpe] :: summonAll[tpes]
93-
case '[EmptyTuple] => Nil
88+
def summonAll[T: Type](using Quotes): List[Expr[Eq[_]]] =
89+
Type.of[T] match
90+
case '[String *: tpes] => '{ summon[Eq[String]] } :: summonAll[tpes]
91+
case '[Int *: tpes] => '{ summon[Eq[Int]] } :: summonAll[tpes]
92+
case '[tpe *: tpes] => derived[tpe] :: summonAll[tpes]
93+
case '[EmptyTuple] => Nil
9494
```
9595

9696
One additional difference with the body of `derived` here as opposed to the one
@@ -101,9 +101,9 @@ class that holds a name of type `String` and an age of type `Int`, the equality
101101
check we want to generate is the following:
102102

103103
```scala
104-
true
105-
&& Eq[String].eqv(x.productElement(0),y.productElement(0))
106-
&& Eq[Int].eqv(x.productElement(1), y.productElement(1))
104+
true
105+
&& Eq[String].eqv(x.productElement(0),y.productElement(0))
106+
&& Eq[Int].eqv(x.productElement(1), y.productElement(1))
107107
```
108108

109109
### Calling the derived method inside the macro
@@ -159,39 +159,39 @@ object Eq:
159159

160160
def summonAll[T: Type](using Quotes): List[Expr[Eq[_]]] =
161161
Type.of[T] match
162-
case '[String *: tpes] => '{ summon[Eq[String]] } :: summonAll[tpes]
163-
case '[Int *: tpes] => '{ summon[Eq[Int]] } :: summonAll[tpes]
164-
case '[tpe *: tpes] => derived[tpe] :: summonAll[tpes]
165-
case '[EmptyTuple] => Nil
162+
case '[String *: tpes] => '{ summon[Eq[String]] } :: summonAll[tpes]
163+
case '[Int *: tpes] => '{ summon[Eq[Int]] } :: summonAll[tpes]
164+
case '[tpe *: tpes] => derived[tpe] :: summonAll[tpes]
165+
case '[EmptyTuple] => Nil
166166

167167
given derived[T: Type](using q: Quotes): Expr[Eq[T]] =
168168
import quotes.reflect._
169169

170170
val ev: Expr[Mirror.Of[T]] = Expr.summon[Mirror.Of[T]].get
171171

172172
ev match
173-
case '{ $m: Mirror.ProductOf[T] { type MirroredElemTypes = elementTypes }} =>
174-
val elemInstances = summonAll[elementTypes]
175-
val eqProductBody: (Expr[T], Expr[T]) => Expr[Boolean] = (x, y) =>
176-
elemInstances.zipWithIndex.foldLeft(Expr(true: Boolean)) {
177-
case (acc, (elem, index)) =>
178-
val e1 = '{$x.asInstanceOf[Product].productElement(${Expr(index)})}
179-
val e2 = '{$y.asInstanceOf[Product].productElement(${Expr(index)})}
180-
181-
'{ $acc && $elem.asInstanceOf[Eq[Any]].eqv($e1, $e2) }
182-
}
183-
'{ eqProduct((x: T, y: T) => ${eqProductBody('x, 'y)}) }
184-
185-
case '{ $m: Mirror.SumOf[T] { type MirroredElemTypes = elementTypes }} =>
186-
val elemInstances = summonAll[elementTypes]
187-
val eqSumBody: (Expr[T], Expr[T]) => Expr[Boolean] = (x, y) =>
188-
val ordx = '{ $m.ordinal($x) }
189-
val ordy = '{ $m.ordinal($y) }
190-
191-
val elements = Expr.ofList(elemInstances)
192-
'{ $ordx == $ordy && $elements($ordx).asInstanceOf[Eq[Any]].eqv($x, $y) }
193-
194-
'{ eqSum((x: T, y: T) => ${eqSumBody('x, 'y)}) }
173+
case '{ $m: Mirror.ProductOf[T] { type MirroredElemTypes = elementTypes }} =>
174+
val elemInstances = summonAll[elementTypes]
175+
val eqProductBody: (Expr[T], Expr[T]) => Expr[Boolean] = (x, y) =>
176+
elemInstances.zipWithIndex.foldLeft(Expr(true: Boolean)) {
177+
case (acc, (elem, index)) =>
178+
val e1 = '{$x.asInstanceOf[Product].productElement(${Expr(index)})}
179+
val e2 = '{$y.asInstanceOf[Product].productElement(${Expr(index)})}
180+
181+
'{ $acc && $elem.asInstanceOf[Eq[Any]].eqv($e1, $e2) }
182+
}
183+
'{ eqProduct((x: T, y: T) => ${eqProductBody('x, 'y)}) }
184+
185+
case '{ $m: Mirror.SumOf[T] { type MirroredElemTypes = elementTypes }} =>
186+
val elemInstances = summonAll[elementTypes]
187+
val eqSumBody: (Expr[T], Expr[T]) => Expr[Boolean] = (x, y) =>
188+
val ordx = '{ $m.ordinal($x) }
189+
val ordy = '{ $m.ordinal($y) }
190+
191+
val elements = Expr.ofList(elemInstances)
192+
'{ $ordx == $ordy && $elements($ordx).asInstanceOf[Eq[Any]].eqv($x, $y) }
193+
194+
'{ eqSum((x: T, y: T) => ${eqSumBody('x, 'y)}) }
195195
end derived
196196
end Eq
197197

docs/docs/reference/contextual/derivation.md

+8-8
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,8 @@ a `Mirror[T]`. Here is a possible implementation,
180180
inline given derived[T](using m: Mirror.Of[T]): Eq[T] =
181181
val elemInstances = summonAll[m.MirroredElemTypes] // (1)
182182
inline m match // (2)
183-
case s: Mirror.SumOf[T] => eqSum(s, elemInstances)
184-
case p: Mirror.ProductOf[T] => eqProduct(p, elemInstances)
183+
case s: Mirror.SumOf[T] => eqSum(s, elemInstances)
184+
case p: Mirror.ProductOf[T] => eqProduct(p, elemInstances)
185185
```
186186

187187
Note that `derived` is defined as an `inline` given. This means that the method will be expanded at
@@ -197,8 +197,8 @@ implementation of `summonAll` is `inline` and uses Scala 3's `summonInline` cons
197197

198198
inline def summonAll[T <: Tuple]: List[Eq[_]] =
199199
inline erasedValue[T] match
200-
case _: EmptyTuple => Nil
201-
case _: (t *: ts) => summonInline[Eq[t]] :: summonAll[ts]
200+
case _: EmptyTuple => Nil
201+
case _: (t *: ts) => summonInline[Eq[t]] :: summonAll[ts]
202202
```
203203

204204
with the instances for children in hand the `derived` method uses an `inline match` to dispatch to methods which can
@@ -238,8 +238,8 @@ import scala.compiletime.{erasedValue, summonInline}
238238

239239
inline def summonAll[T <: Tuple]: List[Eq[_]] =
240240
inline erasedValue[T] match
241-
case _: EmptyTuple => Nil
242-
case _: (t *: ts) => summonInline[Eq[t]] :: summonAll[ts]
241+
case _: EmptyTuple => Nil
242+
case _: (t *: ts) => summonInline[Eq[t]] :: summonAll[ts]
243243

244244
trait Eq[T]:
245245
def eqv(x: T, y: T): Boolean
@@ -269,8 +269,8 @@ object Eq:
269269
inline given derived[T](using m: Mirror.Of[T]): Eq[T] =
270270
lazy val elemInstances = summonAll[m.MirroredElemTypes]
271271
inline m match
272-
case s: Mirror.SumOf[T] => eqSum(s, elemInstances)
273-
case p: Mirror.ProductOf[T] => eqProduct(p, elemInstances)
272+
case s: Mirror.SumOf[T] => eqSum(s, elemInstances)
273+
case p: Mirror.ProductOf[T] => eqProduct(p, elemInstances)
274274
end Eq
275275
```
276276

0 commit comments

Comments
 (0)