All Downloads are FREE. Search and download functionalities are using the official Maven repository.

dotty.tools.dotc.transform.CtxLazy.scala Maven / Gradle / Ivy

There is a newer version: 3.6.4-RC1-bin-20241220-0bfa1af-NIGHTLY
Show newest version
package dotty.tools.dotc
package transform

import core.Contexts.*

import scala.compiletime.uninitialized

/** Utility class for lazy values whose evaluation depends on a context.
 *  This should be used whenever the evaluation of a lazy expression
 *  depends on some context, but the value can be re-used afterwards
 *  with a different context.
 *
 *  A typical use case is a lazy val in a phase object which exists once per root context where
 *  the expression initializing the lazy val depends only on the root context, but not any changes afterwards.
 */
class CtxLazy[T](expr: Context ?=> T) {
  private var myValue: T = uninitialized
  private var forced = false
  def apply()(using Context): T = {
    if (!forced) {
      myValue = expr
      forced = true
    }
    myValue
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy