Disposable Resource Pattern
Asked Answered
B

2

11

Is there anything standardized within the Scala library to support the disposable resource pattern?

I mean something similar to that supported by C# and .NET just to mention one.

For example does official Scala library provide something like this:

trait Disposable { def dispose() }

class Resource extends Disposable

using (new Resource) { r =>

}

Note: I'm aware of this article «Scala finally block closing/flushing resource» but it seems not integrated within the standard lib

Branen answered 20/3, 2013 at 12:12 Comment(0)
T
7

Starting Scala 2.13, the standard library provides a dedicated resource management utility: Using.

You will just have to provide an implicit definition of how to release the resource, using the Releasable trait:

import scala.util.Using
import scala.util.Using.Releasable

case class Resource(field: String)

implicit val releasable: Releasable[Resource] = resource => println(s"closing $resource")

Using(Resource("hello world")) { resource => resource.field.toInt }
// closing Resource(hello world)
// res0: scala.util.Try[Int] = Failure(java.lang.NumberFormatException: For input string: "hello world")

Note that you can place the implicit releasable in the companion object of Resource for clarity.


Note that you can also use Java's AutoCloseable instead of Using.Releasable and thus any Java or Scala object implementing AutoCloseable (such as scala.io.Source or java.io.PrintWriter) can directly be used with Using:

import scala.util.Using

case class Resource(field: String) extends AutoCloseable {
  def close(): Unit = println(s"closing $this")
}

Using(Resource("hello world")) { resource => resource.field.toInt }
// closing Resource(hello world)
// res0: scala.util.Try[Int] = Failure(java.lang.NumberFormatException: For input string: "hello world")
Triacid answered 31/3, 2019 at 18:50 Comment(0)
D
2

At this time you will need to look to the Scala ARM for a common implementation. Though, as you mentioned, it is a separate library.

For more information:

This answer at functional try & catch w/ Scala links to the Loan Pattern on the scala wiki which has code samples. (I am not re-posting the link because link is subject to change)

Using a variable in finally block has a few answers showing ways you could write your own.

Dorm answered 20/1, 2014 at 22:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.