简体   繁体   中英

returning a lazy val in Scala

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM