finally multimethod

Methods

finally(b:Block, cleanup:Fun)experimental Source: stdlib.ngs:223
Add function to run before exiting the block. The functions added will be run in reverse order.
finally(body:Fun, cleanup:Fun) Source: stdlib.ngs:2822
Run cleanup after successful execution of body or exception in body

Parameters

bodyMain code to execute
cleanupCleanup code to execute

Returns

Whatever body call returns

Example

finally(
  { while entry = c_readdir(d) { ... } },
  { ... c_closedir(d) ...}
)
# Alternative function call syntax:
finally(
  body = {
    while entry = c_readdir(d) {
      cb(Path(dirname / entry.d_name, subtype=subtype))
    }
  }
  cleanup = {
    r = c_closedir(d)
    r != 0 throws DirFail('Failed to close directory after listing').set('dirname', dirname)
  }
)