Guest User

Untitled

a guest
Jan 22nd, 2019
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.38 KB | None | 0 0
  1. object TheApp {
  2. lazy val optionalSubsystem = {
  3. // ...
  4. subsystem
  5. }
  6.  
  7. def main(args: Array[String]) {
  8. bootSubsystemA(this)
  9. bootSubsystemB(this)
  10.  
  11. if (/* optionalSubsystem is initialized */) {
  12. // more dependencies
  13. }
  14. }
  15. }
  16.  
  17. class Lazy[A](f: => A, private var option: Option[A] = None) {
  18.  
  19. def apply(): A = option match {
  20. case Some(a) => a
  21. case None => val a = f; option = Some(a); a
  22. }
  23.  
  24. def toOption: Option[A] = option
  25.  
  26. }
  27.  
  28. scala> val optionalSubsystem = new Lazy { "a" }
  29. optionalSubsystem: Lazy[java.lang.String] = Lazy@1210267
  30.  
  31. scala> optionalSubsystem.toOption.isDefined
  32. res1: Boolean = false
  33.  
  34. scala> optionalSubsystem()
  35. res2: java.lang.String = a
  36.  
  37. scala> optionalSubsystem.toOption.isDefined
  38. res12: Boolean = true
  39.  
  40. import scala.language.implicitConversions
  41.  
  42. object Lazy {
  43.  
  44. def lazily[A](f: => A): Lazy[A] = new Lazy(f)
  45.  
  46. implicit def evalLazy[A](l: Lazy[A]): A = l()
  47.  
  48. }
  49.  
  50. class Lazy[A] private(f: => A) {
  51.  
  52. private var option: Option[A] = None
  53.  
  54. def apply(): A = option match {
  55. case Some(a) => a
  56. case None => val a = f; option = Some(a); a
  57. }
  58.  
  59. def isEvaluated: Boolean = option.isDefined
  60.  
  61. }
  62.  
  63. scala> import Lazy._
  64. import Lazy._
  65.  
  66. scala> val optionalSubsystem = lazily { "a" }
  67. optionalSubsystem: Lazy[String] = Lazy@3d0d54
  68.  
  69. scala> optionalSubsystem.isEvaluated
  70. res0: Boolean = false
  71.  
  72. scala> optionalSubsystem: String
  73. res1: String = a
  74.  
  75. scala> optionalSubsystem.isEvaluated
  76. res2: Boolean = true
  77.  
  78. object TheApp {
  79.  
  80. private var _optionalSubsystemInitialized = false
  81.  
  82. def optionalSubsystemInitialized = _optionalSubsystemInitialized
  83.  
  84. lazy val optionalSubsystem = {
  85. _optionalSubsystemInitialized = true
  86. subsystem
  87. }
  88.  
  89. }
  90.  
  91. package lazyside
  92.  
  93. object Lazy
  94.  
  95. class Foo {
  96. lazy val foo = 7
  97. lazy val bar = { Lazy ; 8 }
  98. }
  99.  
  100. object Test extends App {
  101. import scala.reflect.runtime.{ currentMirror => cm }
  102. import scala.reflect.runtime.universe._
  103.  
  104. val x = new Foo
  105.  
  106. // method 1: reflect the underlying field
  107. val im = cm reflect x
  108. val f = (typeOf[Foo] declaration TermName("foo")).asTerm.accessed.asTerm
  109. def foo_? = x synchronized ((im reflectField f).get != 0)
  110.  
  111. def yn(b: Boolean) = if (b) "yes" else "no"
  112. Console println s"Is foo set yet? ${yn(foo_?)}"
  113.  
  114. // method 2: check a benign side effect like a class load
  115. val m = classOf[ClassLoader].getDeclaredMethod("findLoadedClass", classOf[String])
  116. m setAccessible true
  117. def bar_? = (m invoke (x.getClass.getClassLoader, "lazyside.Lazy$")) != null
  118. Console println s"Is bar set yet? ${yn(bar_?)}"
  119.  
  120. Console println s"I see that foo is ${x.foo}."
  121. Console println s"Is foo set yet? ${yn(foo_?)}"
  122. Console println s"I see that bar is ${x.bar}."
  123. Console println s"Is bar set yet? ${yn(bar_?)}"
  124. Console println s"I see that x is loaded by a ${x.getClass.getClassLoader.getClass}"
  125. }
  126.  
  127. Is foo set yet? no
  128. Is bar set yet? no
  129. I see that foo is 7.
  130. Is foo set yet? yes
  131. I see that bar is 8.
  132. Is bar set yet? yes
  133. I see that x is loaded by a class scala.reflect.internal.util.ScalaClassLoader$URLClassLoader
  134.  
  135. object TheApp {
  136. lazy val optionalSubsystem = {
  137. // ...
  138. subsystem
  139. // more dependencies
  140. }
  141.  
  142. def main(args: Array[String]) {
  143. bootSubsystemA(this)
  144. bootSubsystemB(this)
  145. }
  146. }
  147.  
  148. val used = new AtomicBoolean(false)
  149.  
  150. lazy val o: String = {
  151. used.set(true)
  152. "aaa"
  153. }
  154.  
  155. if (used.get()) { /* initialized */ }
Add Comment
Please, Sign In to add comment