You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/docs/reference/other-new-features/explicit-nulls.md
+11-2Lines changed: 11 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -464,10 +464,15 @@ Since the compiler doesn’t know whether `T` is a reference type, it is unable
464
464
to `T`. A `.nn` need to be inserted after `xs.get(0)` by user manually to fix the error, which
465
465
strips the `Nul`l from its type.
466
466
467
-
The intention of this `unsafeNulls` is to give users a better migration path for explicit nulls. Projects for Scala 2 or regular dotty can try this by adding `-Yexplicit-nulls -language:unsafeNulls` to the compile options. A small number of manual modifications are expected. To migrate to full explicit nulls in the future, `-language:unsafeNulls` can be dropped and add `import scala.language.unsafeNulls` only when needed.
467
+
The intention of this `unsafeNulls` is to give users a better migration path for explicit nulls.
468
+
Projects for Scala 2 or regular dotty can try this by adding `-Yexplicit-nulls -language:unsafeNulls`
469
+
to the compile options. A small number of manual modifications are expected. To migrate to the full
470
+
explicit nulls feature in the future, `-language:unsafeNulls` can be dropped and add
471
+
`import scala.language.unsafeNulls` only when needed.
468
472
469
473
```scala
470
474
def f(x: String): String = ???
475
+
def nullOf[T >: Null]: T = null
471
476
472
477
import scala.language.unsafeNulls
473
478
@@ -484,9 +489,13 @@ val c: String = null // Null to String
484
489
val d1: Array[String] = ???
485
490
val d2: Array[String | Null] = d1 // unsafely convert Array[String] to Array[String | Null]
486
491
val d3: Array[String] = Array(null) // unsafe
492
+
493
+
class C[T >: Null <: String] // define a type bound with unsafe conflict bound
494
+
495
+
val n = nullOf[String] // apply a type bound unsafely
487
496
```
488
497
489
-
Without the `unsafeNulls`, all these unsafe operations will not be typechecked.
498
+
Without the `unsafeNulls`, all these unsafe operations will not be type-checked.
490
499
491
500
`unsafeNulls` also works for extension methods and implicit search.
0 commit comments