io.fsq.common.scala.LazyLocal.scala Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of common-scala_2.11 Show documentation
Show all versions of common-scala_2.11 Show documentation
Fsq.io common libraries, from Foursquare.
The newest version!
// Copyright 2014 Foursquare Labs Inc. All Rights Reserved.
package io.fsq.common.scala
/**
* Wrapper for lazy vals used within methods. lazy vals in methods synchronize
* on the object instance, not in the scope of the method. Thus multiple threads
* can block on evaluating the same lazy val in the method.
*
* ALWAYS USE THIS IF YOU WANT TO USE A lazy val IN A METHOD, e.g.
*
*
* def myLazilyEvaluatedFunc(..) = {..}
* val l = LazyLocal(myLazilyEvaluatedFunc)
* ...
* l.value
*
*/
class LazyLocal[T](f: => T) {
lazy val value: T = f
}
object LazyLocal {
def apply[T](f: => T) = {
new LazyLocal(f)
}
}