Skip to content

Commit 772a3c3

Browse files
authored
Update definition of nn to use extension method
The definition of `nn` has [changed](https://github.com/lampepfl/dotty/blob/fad5b8d7d7563768c491846c9cdfcf5b08aa92cf/compiler/src/dotty/tools/package.scala#L31) and uses the new extension syntax, is inlined, and uses `assert`.
1 parent 63cd945 commit 772a3c3

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,10 @@ So far, we have found the following useful:
8181
- An extension method `.nn` to "cast away" nullability
8282

8383
```scala
84-
def[T] (x: T|Null) nn: x.type & T =
85-
if x == null then new NullPointerException("tried to cast away nullability, but value is null")
86-
else x.asInstanceOf[x.type & T]
84+
extension [T](x: T | Null)
85+
inline def nn: T =
86+
assert(x != null)
87+
x.asInstanceOf[T]
8788
```
8889

8990
This means that given `x: String|Null`, `x.nn` has type `String`, so we can call all the

0 commit comments

Comments
 (0)