Skip to content

Commit a7f6c7c

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

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

library/src/scala/runtime/stdLibPatches/Predef.scala

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package scala.runtime.stdLibPatches
22

3+
import scala.annotation.experimental
4+
35
object Predef:
46
import compiletime.summonFrom
57

@@ -47,4 +49,18 @@ object Predef:
4749
*/
4850
extension [T](x: T | Null) inline def nn: x.type & T =
4951
scala.runtime.Scala3RunTime.nn(x)
52+
53+
/** Marker for `var`s that are initialized to `null`, but cannot be assigned `null` after initialization.
54+
* @example {{{
55+
* var cache: String | Uninitialized = initiallyNull
56+
* def readCache: String =
57+
* if(cache == null) cache = "hello"
58+
* cache
59+
* }}}
60+
*/
61+
@experimental
62+
type Uninitialized <: Null
63+
/** Initializer for `var`s of type `Uninitialized` */
64+
@experimental
65+
val initiallyNull = null.asInstanceOf[Uninitialized]
5066
end Predef

0 commit comments

Comments
 (0)