0

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
        }
    }
SergGr
  • 23,570
  • 2
  • 30
  • 51
bhank
  • 11
  • 4
  • Please provide a self-explanatory example, or explain what is happening here. Clarify: What is `resource`, what is `managed`. – 0__ Dec 01 '17 at 22:39
  • The Scala ARM library allows users to ensure opening closing of resources within blocks of code using the managed method. This is easiest to accomplish with a for expression and the managed method defined on scala.resource. – bhank Dec 01 '17 at 22:42
  • The managed method essentially takes an argument of “anything that has a close or dispose method” and constructs a new ManagedResource object. This object has a foreach method which can be used inside of the for expression. The scala-arm library provides a very flexible mechanism for customising the treatment of resource types, using a type class trait. Please read the section on the Resource Type Class for more information. – bhank Dec 01 '17 at 22:42
  • 1
    whats the problem with your solution? – pedrorijo91 Dec 01 '17 at 23:36

1 Answers1

0

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 ExtractableManagedResource which 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

def getFileAsList(bufferedReader: BufferedReader): java.util.stream.Stream[String] = {
  import resource._
  val managedWrapper = for (source <- managed(bufferedReader))
    yield for (line <- source.lines())
      yield line
  managedWrapper.tried.get
}

The tried method converts ExtractableManagedResource into a Try and get on that will either get you the result or (re-)throw the exception that happened during result calculation.

Please also note, that java.util.Stream is a beast quite different from scala.collection.Seq or scala.collection.Stream. If you want get Scala-specific Stream you should use some Scala-specific code such as

def getFileAsList(bufferedReader: BufferedReader): scala.collection.immutable.Stream[String] = {
  import resource._
  val managedWrapper = for (source <- managed(bufferedReader))
    yield Stream.continually(source.readLine()).takeWhile(_ != null)
  managedWrapper.tried.get
}
SergGr
  • 23,570
  • 2
  • 30
  • 51