Skip to content

Commit d313f17

Browse files
committed
Address other review suggestions
1 parent afbcfd7 commit d313f17

File tree

3 files changed

+36
-39
lines changed

3 files changed

+36
-39
lines changed

docs/docs/internals/explicit-nulls.md

+14-19
Original file line numberDiff line numberDiff line change
@@ -111,30 +111,25 @@ The reason for casting to `x.type & T`, as opposed to just `T`, is that it allow
111111
support flow typing for paths of length greater than one.
112112

113113
```scala
114-
abstract class Node {
115-
val x: String
116-
val next: Node | Null
117-
}
118-
119-
def f = {
120-
val l: Node|Null = ???
121-
if (l != null && l.next != null) {
122-
val third: l.next.next.type = l.next.next
123-
}
124-
}
114+
abstract class Node:
115+
val x: String
116+
val next: Node | Null
117+
118+
def f =
119+
val l: Node|Null = ???
120+
if l != null && l.next != null then
121+
val third: l.next.next.type = l.next.next
125122
```
126123

127124
After typing, `f` becomes:
128125

129126
```scala
130-
def f = {
131-
val l: Node|Null = ???
132-
if (l != null && l.$asInstanceOf$[l.type & Node].next != null) {
133-
val third:
134-
l.$asInstanceOf$[l.type & Node].next.$asInstanceOf$[(l.type & Node).next.type & Node].next.type =
135-
l.$asInstanceOf$[l.type & Node].next.$asInstanceOf$[(l.type & Node).next.type & Node].next
136-
}
137-
}
127+
def f =
128+
val l: Node|Null = ???
129+
if l != null && l.$asInstanceOf$[l.type & Node].next != null then
130+
val third:
131+
l.$asInstanceOf$[l.type & Node].next.$asInstanceOf$[(l.type & Node).next.type & Node].next.type =
132+
l.$asInstanceOf$[l.type & Node].next.$asInstanceOf$[(l.type & Node).next.type & Node].next
138133
```
139134
Notice that in the example above `(l.type & Node).next.type & Node` is still a stable path, so
140135
we can use it in the type and track it for flow typing.

docs/docs/reference/metaprogramming/macros.md

+16-16
Original file line numberDiff line numberDiff line change
@@ -466,8 +466,8 @@ inline def power(x: Double, inline n: Int) = ${ powerCode('x, 'n) }
466466

467467
private def powerCode(x: Expr[Double], n: Expr[Int])(using Quotes): Expr[Double] =
468468
n.value match
469-
case Some(m) => powerCode(x, m)
470-
case None => '{ Math.pow($x, $n.toDouble) }
469+
case Some(m) => powerCode(x, m)
470+
case None => '{ Math.pow($x, $n.toDouble) }
471471

472472
private def powerCode(x: Expr[Double], n: Int)(using Quotes): Expr[Double] =
473473
if n == 0 then '{ 1.0 }
@@ -613,8 +613,8 @@ inline def setFor[T]: Set[T] = ${ setForExpr[T] }
613613

614614
def setForExpr[T: Type](using Quotes): Expr[Set[T]] =
615615
Expr.summon[Ordering[T]] match
616-
case Some(ord) => '{ new TreeSet[T]()($ord) }
617-
case _ => '{ new HashSet[T] }
616+
case Some(ord) => '{ new TreeSet[T]()($ord) }
617+
case _ => '{ new HashSet[T] }
618618
```
619619

620620
## Relationship with Whitebox Inline
@@ -628,8 +628,8 @@ transparent inline def defaultOf(inline str: String) = ${ defaultOfImpl('str) }
628628

629629
def defaultOfImpl(strExpr: Expr[String])(using Quotes): Expr[Any] =
630630
strExpr.valueOrError match
631-
case "int" => '{1}
632-
case "string" => '{"a"}
631+
case "int" => '{1}
632+
case "string" => '{"a"}
633633

634634
// in a separate file
635635
val a: Int = defaultOf("int")
@@ -665,16 +665,16 @@ These could be used in the following way to optimize any call to `sum` that has
665665
inline def sum(inline args: Int*): Int = ${ sumExpr('args) }
666666
private def sumExpr(argsExpr: Expr[Seq[Int]])(using Quotes): Expr[Int] =
667667
argsExpr match
668-
case Varargs(args @ Exprs(argValues)) =>
669-
// args is of type Seq[Expr[Int]]
670-
// argValues is of type Seq[Int]
671-
Expr(argValues.sum) // precompute result of sum
672-
case Varargs(argExprs) => // argExprs is of type Seq[Expr[Int]]
673-
val staticSum: Int = argExprs.map(_.value.getOrElse(0)).sum
674-
val dynamicSum: Seq[Expr[Int]] = argExprs.filter(_.value.isEmpty)
675-
dynamicSum.foldLeft(Expr(staticSum))((acc, arg) => '{ $acc + $arg })
676-
case _ =>
677-
'{ $argsExpr.sum }
668+
case Varargs(args @ Exprs(argValues)) =>
669+
// args is of type Seq[Expr[Int]]
670+
// argValues is of type Seq[Int]
671+
Expr(argValues.sum) // precompute result of sum
672+
case Varargs(argExprs) => // argExprs is of type Seq[Expr[Int]]
673+
val staticSum: Int = argExprs.map(_.value.getOrElse(0)).sum
674+
val dynamicSum: Seq[Expr[Int]] = argExprs.filter(_.value.isEmpty)
675+
dynamicSum.foldLeft(Expr(staticSum))((acc, arg) => '{ $acc + $arg })
676+
case _ =>
677+
'{ $argsExpr.sum }
678678
```
679679

680680
### Quoted patterns

docs/docs/reference/other-new-features/explicit-nulls.md

+6-4
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,8 @@ Specifically, we patch
106106

107107
```java
108108
class C {
109-
String s;
110-
int x;
109+
String s;
110+
int x;
111111
}
112112
```
113113
==>
@@ -183,12 +183,13 @@ Specifically, we patch
183183
* We don't nullify _simple_ literal constant (`final`) fields, since they are known to be non-null
184184

185185
```java
186-
class Constants:
186+
class Constants {
187187
final String NAME = "name";
188188
final int AGE = 0;
189189
final char CHAR = 'a';
190190

191191
final String NAME_GENERATED = getNewName();
192+
}
192193
```
193194
==>
194195
```scala
@@ -204,10 +205,11 @@ Specifically, we patch
204205
`NotNull` annotation.
205206

206207
```java
207-
class C:
208+
class C {
208209
@NotNull String name;
209210
@NotNull List<String> getNames(String prefix); // List is Java-defined
210211
@NotNull Box<String> getBoxedName(); // Box is Scala-defined
212+
}
211213
```
212214
==>
213215
```scala

0 commit comments

Comments
 (0)