org.specs.util.LazyParameters.scala Maven / Gradle / Ivy
package org.specs.util
/**
* This trait can be used to allow some function to be called with varargs, with
* values being evaluated lazily:
*
* def method[T](values: LazyParameter[T]*) = {
* values.toStream // use the toStream method to consume the values lazily
* }
* // usage
* method(exp1, exp2, exp3)
*
* Note that the values are really evaluated once, unlike a by-name parameter.
* @see org.specs.util.lazyParamSpec
*/
trait LazyParameters {
/** transform a value to a zero-arg function returning that value */
implicit def toLazyParameter[T](value: =>T) = new LazyParameter(() => value)
}
/** class holding a value to be evaluated lazily */
class LazyParameter[T](value: ()=>T) {
lazy val v = value()
def apply() = v
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy