A description of how to acquire and release a resource.
Resources encapsulate both acquisition and finalization, tying a value's lifecycle to a Scope. When a resource is created via scope.allocate(resource), the scope registers any finalizers and ensures they run when the scope closes.
Resources are lazy—they describe ''what'' to do, not ''when''. Creation only happens when passed to a scope via scope.allocate(resource).
==Creating Resources==
Resource(=> a): By-name value; auto-registers close() if AutoCloseable
final def use[B](f: A=>B)(implicit ev: Unscoped[B]): B
Acquires this resource in Scope.global, uses it, and closes it when the callback returns.
Acquires this resource in Scope.global, uses it, and closes it when the callback returns.
This provides a lighter-weight entrypoint for application code while preserving the same finalization semantics as explicit Scope.global.scoped { scope => ... } usage.
Type parameters
B
result type; must be Unscoped so only pure data escapes the scope
Value parameters
f
callback that uses the acquired value
Attributes
Returns
the callback result after all finalizers have run
Example
val serverResource: Resource[HttpServer] = ???
serverResource.use(_.start())