4.7. kink/DYN

Provides dynamic scope variables.

You can bind an arbitrary val to a dyn using Dyn.bind.

You can retrieve all the bound vals using Dyn.all, and the most recent val using Dyn.top.

DYN can be used to bear the runtime context like transaction. See the following example:

:DYN.require_from('kink/')

:Tx_dyn <- DYN.new

:do_transaction <- {(:Tx_name :thunk)
  Tx_dyn.bind(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
}

4.7.1. type dyn

A dyn is a dynamic scope variable. Binding can be nested.

4.7.1.1. Dyn.bind(Bound $thunk)

`bind` binds `Bound` to `Dyn` within invocation of $thunk. `bind` returns the result of $thunk.

4.7.1.2. Dyn.all

`all` returns a vec of vals bound to `Dyn`, in the order from the root of the execution stack to the leaf.

Example:

:DYN.require_from('kink/')

:Dyn <- DYN.new

Dyn.bind('foo'){
  Dyn.bind('bar'){
    Dyn.bind('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) # => []

4.7.1.3. Dyn.top

`top` returns the val bound to `Dyn` which is closest to the leaf of the execution stack.

Precondition:

• At least one val must be bound to `Dyn`.

Example:

:DYN.require_from('kink/')

:Dyn <- DYN.new

Dyn.bind('foo'){
  stdout.print_line(Dyn.top.repr) # => "foo"
}
Dyn.top # => raises an exception

4.7.1.4. Dyn.empty?

`empty?` returns whether at least one val is bound to `Dyn`.

Example:

:DYN.require_from('kink/')

:Dyn <- DYN.new

Dyn.bind('foo'){
  stdout.print_line(Dyn.empty?.repr) # => false
}
stdout.print_line(Dyn.empty?.repr) # => true

4.7.2. DYN.new

`new` makes a new dyn.

4.7.3. DYN.is?(Val)

`is?` returns whether `Val` is a dyn.