Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disposable Resource Pattern

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

like image 794
Lord of the Goo Avatar asked Mar 20 '13 12:03

Lord of the Goo


People also ask

What is disposable programming?

In the context of C#, dispose is an object method invoked to execute code required for memory cleanup and release and reset unmanaged resources, such as file handles and database connections.

What happens if Dispose is not called?

Implement a finalizer to free resources when Dispose is not called. By default, the garbage collector automatically calls an object's finalizer before reclaiming its memory. However, if the Dispose method has been called, it is typically unnecessary for the garbage collector to call the disposed object's finalizer.

What are unmanaged resources?

The most common types of unmanaged resources are objects that wrap operating system resources, such as files, windows, network connections, or database connections.

When dispose method is called in C#?

When the close brace is reached, the Dispose( ) method will be called on the object automatically, as illustrated in Example 4-6. In the first part of this example, the Font object is created within the using statement. When the using statement ends, Dispose( ) is called on the Font object.


1 Answers

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")
like image 91
Xavier Guihot Avatar answered Nov 09 '22 05:11

Xavier Guihot