Skip to content

Commit c2775b0

Browse files
committed
add tools.Uninitialized for vars that are null only initially
1 parent d02c7c2 commit c2775b0

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

compiler/src/dotty/tools/package.scala

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,16 @@ package object tools {
5454

5555
def unreachable(x: Any = "<< this case was declared unreachable >>"): Nothing =
5656
throw new MatchError(x)
57+
58+
/** Marker for `var`s that are initialized to `null`, but cannot be assigned `null` after initialization.
59+
* @example {{{
60+
* var cache: String | Uninitialized = initiallyNull
61+
* def readCache: String =
62+
* if(cache == null) cache = "hello"
63+
* cache
64+
* }}}
65+
*/
66+
type Uninitialized <: Null
67+
/** Initializer for `var`s of type `Uninitialized` */
68+
val initiallyNull = null.asInstanceOf[Uninitialized]
5769
}

0 commit comments

Comments
 (0)