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