Skip to content

Commit 3d64f51

Browse files
authored
Merge pull request #11154 from ysthakur/patch-1
Update definition of `nn` to use extension method
2 parents c63bd9a + 772a3c3 commit 3d64f51

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)