From a7f6c7c8521fb8d210040bc2d9486ea928cb1a38 Mon Sep 17 00:00:00 2001 From: Ondrej Lhotak Date: Thu, 5 May 2022 22:37:26 +0200 Subject: [PATCH] add Predef.Uninitialized for vars that are null only initially --- .../src/scala/runtime/stdLibPatches/Predef.scala | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/library/src/scala/runtime/stdLibPatches/Predef.scala b/library/src/scala/runtime/stdLibPatches/Predef.scala index 13dfc77ac60b..95ea628c9b0a 100644 --- a/library/src/scala/runtime/stdLibPatches/Predef.scala +++ b/library/src/scala/runtime/stdLibPatches/Predef.scala @@ -1,5 +1,7 @@ package scala.runtime.stdLibPatches +import scala.annotation.experimental + object Predef: import compiletime.summonFrom @@ -47,4 +49,18 @@ object Predef: */ extension [T](x: T | Null) inline def nn: x.type & T = scala.runtime.Scala3RunTime.nn(x) + + /** Marker for `var`s that are initialized to `null`, but cannot be assigned `null` after initialization. + * @example {{{ + * var cache: String | Uninitialized = initiallyNull + * def readCache: String = + * if(cache == null) cache = "hello" + * cache + * }}} + */ + @experimental + type Uninitialized <: Null + /** Initializer for `var`s of type `Uninitialized` */ + @experimental + val initiallyNull = null.asInstanceOf[Uninitialized] end Predef