tur/unique
stdlib/unique.tur
uniqueness-type patterns (with-unique, consume, replace).
Since: UT3
defmacro
with-unique
(with-unique [binding & body])
scope a uniquely-owned binding over a body.
Parameters
| binding | a two-element form `[name init]`: the binding name and its | |
| uniquely-owned initializer | ||
| body | one or more expressions evaluated with `name` in scope |
Returns
The value of the last body expression.
Example
(with-unique [u (make-ref 42)] ; u : ^unique
(use-once u)) ; consumed exactly once
Since: UT3
defn
consume
(consume [A B])
thread a unique value through a single transforming call.
Parameters
| x | the uniquely-owned value to consume | |
| f | a function that takes ownership of `x` and returns a result |
Returns
The result of `(f x)`.
Example
(consume buf buf-finish) ; => the result of (buf-finish buf)
Since: UT3
defn
replace
(replace [A])
displace a unique value, returning the old one.
Parameters
| old | the uniquely-owned value being displaced | |
| new | the replacement value, consumed into the swap |
Returns
The displaced `old` value, for the caller to inspect or drop.
Example
(let [displaced (replace current next)]
(drop! displaced)) ; old `current` handed back for disposal
Since: UT3