returning a lazy val in Scala
I have a function that looks like this:
package org.thimblr.io
import java.i开发者_如何学JAVAo._
object Local {
def streamer(path: String) = () => new FileReader(path)
}
This manages basically what I want to do, which is to return a function that opens a stream from a file when it's called. So client code can do this:
val planStreamSource = Local.streamer("/home/someuser/.plan")
//...passes the function on to somewhere else
val planStream = planStreamSource()
val firstByte = planStream.read
//can now read from planStream
But what I'd really like is to return a lazy val that streams from a file once it's referenced, like this:
val planStream = Local.streamer("/home/someuser/.plan")
//...passes the val on to somewhere else, without opening the file for reading yet
val firstByte=planStream.read
//...such that planStream was only just opened to allow the read
Is it possible to do something like this, return a lazy val, so that client code can treat it as a value rather than a function?
You can't “return a lazy val” — client code must declare it as lazy. If you don't want to force the client to declare a lazy val, you could return a wrapper instead:
class LazyWrapper[T](wrp: => T) {
lazy val wrapped: T = wrp
}
object LazyWrapper {
implicit def unboxLazy[T](wrapper: LazyWrapper[T]): T = wrapper.wrapped
}
And then:
def streamer(path: String) = new LazyWrapper(new FileReader(path))
You could further forward equals
, hashCode
, etc. to the wrapped object in LazyWrapper
if you need those.
精彩评论