6.10. kink/DYN¶
6.10.1. type dyn¶
A `dyn` is a tag of values which are pushed and popped on the call stack.
The name `dyn` comes from “dynamic scope”, as it acts like dynamically scoped variables of some dialects of Lisp.
A `dyn` can be used to carry runtime context, such as transactions. See the following example:
:DYN.require_from('kink/')
:Tx_dyn <- DYN.new
:do_transaction <- {(:Tx_name :thunk)
Tx_dyn.with(Tx_name){
thunk
}
}
:commit <- {()
:Current_tx = Tx_dyn.top
stdout.print_line('commit {}'.format(Current_tx))
}
do_transaction('Outer'){
commit # => commit Outer
do_transaction('Inner'){
commit # => commit Inner
}
commit # => commit Outer
}
6.10.1.1. Dyn.with(Val $thunk)¶
`with` pushes `Val`, associating it with `Dyn`, on the call stack, and calls $thunk with no arg. When the invocation of $thunk exits, `Val` is popped from the call stack.
Precondition
$thunk must be a thunk.
6.10.1.2. Dyn.all¶
`all` returns a vec of values associated with `Dyn`, in the order from the bottom of the call stack to the top of the call stack.
Example
:DYN.require_from('kink/')
:Dyn <- DYN.new
Dyn.with('foo'){
Dyn.with('bar'){
Dyn.with('baz'){
stdout.print_line(Dyn.all.repr) # => ["foo" "bar" "baz"]
}
stdout.print_line(Dyn.all.repr) # => ["foo" "bar"]
}
stdout.print_line(Dyn.all.repr) # => ["foo"]
}
stdout.print_line(Dyn.all.repr) # => []
6.10.1.3. Dyn.top¶
`top` returns the value associated with `Dyn` which is closest to the top of the call stack.
Precondition
At least one value must be associated with `Dyn` in the call stack.
Example
:DYN.require_from('kink/')
:Dyn <- DYN.new
Dyn.with('foo'){
stdout.print_line(Dyn.top.repr) # => "foo"
}
Dyn.top # => raises an exception
6.10.1.4. Dyn.any?¶
`any?` returns whether any value is associated with `Dyn` in the call stack.
Example
:DYN.require_from('kink/')
:Dyn <- DYN.new
Dyn.with('foo'){
stdout.print_line(Dyn.any?.repr) # => true
}
stdout.print_line(Dyn.any?.repr) # => false
6.10.2. DYN.new¶
`new` makes a new `dyn`.
6.10.3. DYN.is?(Val)¶
`is?` returns whether `Val` is a `dyn`.