Module Thread_safe_pipe.If_closed
Functions that write elements to the pipe take an If_closed.t
argument to specify how to deal with the possibility that the pipe is closed.
The alternatives are to Raise
on a closed pipe, or Return
a variant indicating whether the pipe is closed. This allows lightweight syntax for calls that want to raise if the pipe is closed:
write t a ~if_closed:Raise
It also allows lightweight syntax for calls that want to match on whether the pipe was closed:
match write t a ~if_closed:Return with
| Closed -> ...
| Written -> ...
Returning a variant is essential when one wants to distinguish a closed pipe from other errors. Also, since pipe-writing functions acquire the Async lock, it would be incorrect (due to races) to check is_closed
prior to the lock acquisition.
type 'a t
=
|
Raise : unit t
|
Return : Written_or_closed.t t