call_with_stream(s, fn)
Call the function fn
with the input or output stream s
as its only argument.
If fn
returns normally, close s
and return the value of fn
.
If fn
throws an error, close s
and re-throw the error.
The stream represented by s
is not automatically closed if a continuation created outside of fn
is invoked, since it is possible that another continuation created inside of fn
will be invoked
at a later time, returning control to fn
and expecting that s
is still open.
// write a file:
call_with_stream(file_writer("abc"), ^(s) println(stream = s, "hello, world"))
// read it back:
call_with_stream(file_reader("abc"), ^(s) read_line(s))
// hello world