use - brombres/Rogue GitHub Wiki
Syntax
use v1=resource_provider_1 [, v2=resource_provider_2, ...]
... # By default, 'use' is implicitly scoped to end of current block
use v1=resource_provider_1 [, v2=resource_provider_2, ...]
... # optional 'endUse' explicitly ends scope of 'use'.
endUse
Description
- At its core ,
useassigns resources to local variables within the scope of theusecommand. - If an assigned variable already exists, its value is preserved and restored at the
endUsecommand. - The resource must implement the Use Protocol, described below.
Use Protocol
use will automatically call the following methods on a resource provider:
on_use()->SomeType
Writing use v = resource_provider becomes local v = resource_provider.on_use().
on_end_use(v:SomeType)
Returns the acquired resource to the provider.
Description
- The used resource will be correctly released if there are any returns, escapes, or exceptions thrown within the
usestatements.
Example
use builder = String.pool
builder.print "Hello World!"
println builder
# Equivalent to
local provider = String.pool
local builder = provider.on_use
builder.print "Hello World!"
println builder
provider.on_end_use( builder )