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

scalan.Lazy.scala Maven / Gradle / Ivy

The newest version!
package scalan

/** Non-thread safe (but efficient on single thread) immutable lazy value.
  * The `block` is executed only once. */
class Lazy[A] private (block: => A) {
  @volatile private[this] var _isSet: Boolean = false
  private[this] var _value: A = _

  def value: A = {
    if (!_isSet) {
      _value = block
      _isSet = true
    }
    _value
  }
  
  @inline def isSet = _isSet
  
  override def toString = {
    if (!_isSet)
      ""
    else
      value.toString
  }
}

object Lazy {
  def apply[A](block: => A): Lazy[A] = new Lazy(block)
}








© 2015 - 2024 Weber Informatics LLC | Privacy Policy