Skip to content

Commit 5a9b30b

Browse files
committed
Add an example
1 parent 53c623e commit 5a9b30b

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
@@ -464,10 +464,15 @@ Since the compiler doesn’t know whether `T` is a reference type, it is unable
464464
to `T`. A `.nn` need to be inserted after `xs.get(0)` by user manually to fix the error, which
465465
strips the `Nul`l from its type.
466466

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

469473
```scala
470474
def f(x: String): String = ???
475+
def nullOf[T >: Null]: T = null
471476

472477
import scala.language.unsafeNulls
473478

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

0 commit comments

Comments
 (0)