I have a function that looks like this:
package org.thimblr.io
import java.io._
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With