scala-arm
This project aims to be the Scala Incubator project for Automatic-Resource-Management in the scala library
The Scala Programming Language
I'm very new to Scala, so please excuse my basic question.
I'm trying to get Scala-Arm to manage the release of a class member, but having trouble with scoping.
Although the following code compiles, it fails with
java.lang.NoSuchMethodError: resource.ManagedResource.reflect()Ljava/lang/Object;
on the reflect call. So it seems the the managed_file
is being released immediately.
For local variables I have used the for (managed_file <- managed(...))
syntax but I can't see what to do here.
Suggestions anyone?
class Writer(outputPath: String){
val managed_file = managed(new FileOutputStream(new File(outputPath)))
def write(something : SomeClass) {
var bos = new ByteArrayOutputStream
var dos = new DataOutputStream(bos)
try {
dos.write(Marshal.dump(something))
bos.writeTo(managed_file.reflect)
}
finally {
dos.close
bos.close
}
}
}
Source: (StackOverflow)
I'm using scala-arm library to automatically release/close resources (for example InputStream
).
But the problem is that code below returns ExtractableManagedResource[Int]
, not just Int
as I want.
val result = for(responseStream <- managed(response.getResponseBodyAsStream)) yield {
val localResult: Int = 1
localResult
}
// result is of type ExtractableManagedResource[Int]
Is there any option to return Int
and overcome wrapping result to ExtractableManagedResource
?
EDIT: I know that I can just declre result
variable as var
and assign to it from inside the for-comprehension
, but I want more scala-idiomatic way, i.e. without using var
Source: (StackOverflow)
Following the example on the ScalaARM homepage I wrote this:
for ( ir: IndexReader <- managed(DirectoryReader.open(FSDirectory.open(file)))) { ... }
(Open a lucene IndexReader)
however I get this compilation error:
Error:(34, 45) Play 2 Compiler: Indexer.scala:34: value filter is not
a member of
resource.ManagedResource[org.apache.lucene.index.DirectoryReader]
for ( ir: IndexReader <- managed(DirectoryReader.open(FSDirectory.open(file)))) {
Can someone explain what is happening here?
^
Source: (StackOverflow)