On this page:
Closeable.let
Closeable
Closeable.close
8.15.0.12

9.8 Closeables🔗ℹ

The Closeable.let definition form binds a name to a Closeable object and injects a call to its close method to close the object after subsequent definitions and expressions. Implementing the Closeable interface allows an object to cooperate with Closeable.let.

Evaluates rhs_expr to get a Closeable object and defines bind as the result for use in the body sequence (which must be non-empty). After the body sequence completes, the object is closed with its close method before returning the results of the body sequence.

The rhs_expr is evaluated with breaks disabled like the ~initially part of try. If control escapes from the body sequence (e.g., because an exception is raised), then the object produced by rhs_expr is closed before escaping, and breaks are disabled during that close as in the ~finally part of try. Consequently, in the case of a break exception, the object is reliably closed or not yet opened by rhs_expr. A continuation jump back into the body sequence is disallowed via Continuation.barrier.

> block:

    Closeable.let i = Port.Input.open_file("data.txt")

    i.read_line()

    // `i` is closed after this point

"file content"

An interface that a class can implement (publicly or privately) to customize the way its objects close, especially with Closeable.let.

The Closeable interface has one method:

method

method (c :: Closeable).close()

 

function

fun Closeable.close(c :: Closeable) :: Void

Closes c using its close implementation. The function Closeable.close ignores the method’s results and returns #void regardless.