3.4. kink/CONTROL

The mod which provides various control funs.

3.4.1. kink/CONTROL.escape(Kont_tag Result)

Escapes from continuation boundary with the tag specified by Kont_tag, with the result specified as Result. On escaping, cleanup thunks registered by Fun.ensure are invoked from inner ones to outer ones.

This fun is intended to be used by funs which aborts the control and does not expect resume operations. The examples are reraise or Java_val.throw.

3.4.2. kink/CONTROL.with_on_exit($body)

Calls $body with $on_exit fun. $on_exit takes a thunk, which is registered as a cleanup thunk of the subsequent calculation in the invocation of with_on_exit. The result of $body shall be the result of with_on_exit.

Typical usage of with_on_exit is releasing resources safely.

For example:

:CONTROL.require_from('kink/')
:FILE.require_from('kink/')

:Text <- CONTROL.with_on_exit{(:on_exit)
  :In = FILE.open_to_read('/path/to/file')
  on_exit{ In.close }
  In.read_all_bin.decode('UTF-8')
}
print(Text)  # prints the text in /path/to/file

In the example above, In is closed when exiting from with_on_exit, no matter if read_all_bin returns normally or raises an exception.

3.4.3. kink/CONTROL.with_break($body)

Calls $body with $break fun. $break takes a val. If $break is called in the invocation of $body, it escapes from the invocation of .with_break, and .with_break returns the arg of $break.

Example:

:CONTROL.require_from('kink/')
:Num = 35
:Result = CONTROL.with_break{(:break)
  (Num % 15 == 0).if_true{ break('fizzbuzz') }
  (Num % 3 == 0).if_true{ break('fizz') }
  (Num % 5 == 0).if_true{ break('buzz') }
  Num.show
}
stdout.print_line(Result)  # => buzz