Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- object TheApp {
- lazy val optionalSubsystem = {
- // ...
- subsystem
- }
- def main(args: Array[String]) {
- bootSubsystemA(this)
- bootSubsystemB(this)
- if (/* optionalSubsystem is initialized */) {
- // more dependencies
- }
- }
- }
- class Lazy[A](f: => A, private var option: Option[A] = None) {
- def apply(): A = option match {
- case Some(a) => a
- case None => val a = f; option = Some(a); a
- }
- def toOption: Option[A] = option
- }
- scala> val optionalSubsystem = new Lazy { "a" }
- optionalSubsystem: Lazy[java.lang.String] = Lazy@1210267
- scala> optionalSubsystem.toOption.isDefined
- res1: Boolean = false
- scala> optionalSubsystem()
- res2: java.lang.String = a
- scala> optionalSubsystem.toOption.isDefined
- res12: Boolean = true
- import scala.language.implicitConversions
- object Lazy {
- def lazily[A](f: => A): Lazy[A] = new Lazy(f)
- implicit def evalLazy[A](l: Lazy[A]): A = l()
- }
- class Lazy[A] private(f: => A) {
- private var option: Option[A] = None
- def apply(): A = option match {
- case Some(a) => a
- case None => val a = f; option = Some(a); a
- }
- def isEvaluated: Boolean = option.isDefined
- }
- scala> import Lazy._
- import Lazy._
- scala> val optionalSubsystem = lazily { "a" }
- optionalSubsystem: Lazy[String] = Lazy@3d0d54
- scala> optionalSubsystem.isEvaluated
- res0: Boolean = false
- scala> optionalSubsystem: String
- res1: String = a
- scala> optionalSubsystem.isEvaluated
- res2: Boolean = true
- object TheApp {
- private var _optionalSubsystemInitialized = false
- def optionalSubsystemInitialized = _optionalSubsystemInitialized
- lazy val optionalSubsystem = {
- _optionalSubsystemInitialized = true
- subsystem
- }
- }
- package lazyside
- object Lazy
- class Foo {
- lazy val foo = 7
- lazy val bar = { Lazy ; 8 }
- }
- object Test extends App {
- import scala.reflect.runtime.{ currentMirror => cm }
- import scala.reflect.runtime.universe._
- val x = new Foo
- // method 1: reflect the underlying field
- val im = cm reflect x
- val f = (typeOf[Foo] declaration TermName("foo")).asTerm.accessed.asTerm
- def foo_? = x synchronized ((im reflectField f).get != 0)
- def yn(b: Boolean) = if (b) "yes" else "no"
- Console println s"Is foo set yet? ${yn(foo_?)}"
- // method 2: check a benign side effect like a class load
- val m = classOf[ClassLoader].getDeclaredMethod("findLoadedClass", classOf[String])
- m setAccessible true
- def bar_? = (m invoke (x.getClass.getClassLoader, "lazyside.Lazy$")) != null
- Console println s"Is bar set yet? ${yn(bar_?)}"
- Console println s"I see that foo is ${x.foo}."
- Console println s"Is foo set yet? ${yn(foo_?)}"
- Console println s"I see that bar is ${x.bar}."
- Console println s"Is bar set yet? ${yn(bar_?)}"
- Console println s"I see that x is loaded by a ${x.getClass.getClassLoader.getClass}"
- }
- Is foo set yet? no
- Is bar set yet? no
- I see that foo is 7.
- Is foo set yet? yes
- I see that bar is 8.
- Is bar set yet? yes
- I see that x is loaded by a class scala.reflect.internal.util.ScalaClassLoader$URLClassLoader
- object TheApp {
- lazy val optionalSubsystem = {
- // ...
- subsystem
- // more dependencies
- }
- def main(args: Array[String]) {
- bootSubsystemA(this)
- bootSubsystemB(this)
- }
- }
- val used = new AtomicBoolean(false)
- lazy val o: String = {
- used.set(true)
- "aaa"
- }
- if (used.get()) { /* initialized */ }
Add Comment
Please, Sign In to add comment