trait
ManagedResource[T] extends Any
Abstract Value Members
-
-
abstract
def
getClass(): Class[_]
Concrete Value Members
-
final
def
!=(arg0: Any): Boolean
-
final
def
##(): Int
-
final
def
==(arg0: Any): Boolean
-
final
def
asInstanceOf[T0]: T0
-
def
equals(arg0: Any): Boolean
-
def
hashCode(): Int
-
final
def
isInstanceOf[T0]: Boolean
-
def
toString(): String
An automatically managed resource which is closed before the code block handling it returns. This is intended to be used in a for-comprehension such as:
which will call
x.close()
(or an equivalent method via an AutoResource subclass) when the code block goes out of scope. It is also legal to use the monad methods directly, such as the following equivalent code to the above:though the for-comprehension is usually more intuitive, and provides a more concise syntax for situations where multiple resources are being managed at the same time. Direct use of the methods
foreach
andmap
work well as pass-through operations, where a function object instance is already available.Generally this class is implemented by an implicit conversion which has
ManagedResource[T]
in its return type signature rather thanAutoResource[T]
, while the actual implementation class actually is a subclass of AutoResource. This avoids an extra layer of allocation, while hiding theforeach
,flatMap
, andmap
methods which may not be desirable on arbitrary types:However, in some situations it may be more useful to have the returned AutoResource be defined by something else, such as a common trait or superclass which is not itself exposed to implicit conversion to ManagedResource. In that case, a value class implementation can be used to avoid the overhead of an extra layer of objects:
type of the contained resource