Tried this to create a seq from file:
def getFileAsList(bufferedReader: BufferedReader): Seq[String] ={
import resource._
for(source <- managed(bufferedReader)){
for(line<-source.lines())
yield line
}
}
I don't think you use Scala-ARM in a way it was designed to be used. The thing is that unless you use Imperative style i.e. consume your managed resource in place, you use Monadic style so what you get is result wrapped into a
ExtractableManagedResourcewhich is a delayed (lazy) computation rather than an immediate result. So this is not a direct substitute for Java try-with-resource construct. Monadic style is more useful if you have a method that wants to return some lazy resource that is also happens to be managed i.e. requires some kind of explicit close after usage. But this means that the managed resource is created inside the method rather than passed from the outside as in your case.Still you probably can achieve something similar to what you want with a construction like
The
triedmethod convertsExtractableManagedResourceinto aTryandgeton that will either get you the result or (re-)throw the exception that happened during result calculation.Please also note, that
java.util.Streamis a beast quite different fromscala.collection.Seqorscala.collection.Stream. If you want get Scala-specificStreamyou should use some Scala-specific code such as