scala-arm. Return type

419 Views Asked by At

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

2

There are 2 best solutions below

0
Andrew Norman On BEST ANSWER

this is easier to implement with the monadic approach by using the aquireAndGet feature

managed(response.getResponseBodyAsStream) acquireAndGet { 
responseStream =>
    val localResult: Int = 1
    localResult
}
1
sjrd On

From the documentation:

result.opt.get

Refer to your own link, under the title "Monadic style", for further details.