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

9.10 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.

definition

Closeable.let binds = rhs_expr

body

...

 

definition

Closeable.let binds: rhs_body ...

body

...

 

binds

 = 

bind

 | 

(bind, ...)

Evaluates rhs_expr or the rhs_body sequence 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.

If binds is a parenthesized sequence of bind forms, then rhs_expr or the rhs_body sequence must produce the corresponding number of values, and each value must be a Closeable object.

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.