Skip to content

Commit c9ea33f

Browse files
committed
Add an example
1 parent d5ce260 commit c9ea33f

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -465,10 +465,15 @@ Since the compiler doesn’t know whether `T` is a reference type, it is unable
465465
to `T`. A `.nn` need to be inserted after `xs.get(0)` by user manually to fix the error, which
466466
strips the `Nul`l from its type.
467467

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.
469473

470474
```scala
471475
def f(x: String): String = ???
476+
def nullOf[T >: Null]: T = null
472477

473478
import scala.language.unsafeNulls
474479

@@ -485,9 +490,13 @@ val c: String = null // Null to String
485490
val d1: Array[String] = ???
486491
val d2: Array[String | Null] = d1 // unsafely convert Array[String] to Array[String | Null]
487492
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
488497
```
489498
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.
491500
492501
`unsafeNulls` also works for extension methods and implicit search.
493502

0 commit comments

Comments
 (0)