call-with-port(3scm) Scheme Programmer's Manual call-with-port(3scm)

call-with-port - call a procedure with a port, closing it afterwards

(import (rnrs))                     ;R6RS
(import (rnrs io ports))            ;R6RS
(import (scheme base))              ;R7RS

(call-with-port port proc)

Calls proc with port as an argument. If proc returns, port is closed automatically and the values returned by proc are returned.

If proc does not return, port is not closed automatically, except perhaps when it is possible to prove that port will never again be used for an input or output operation.

Returns whatever values proc returns.

(call-with-port (open-input-file "config.scm") read)

There are also examples in the put-bytevector(3scm) and put-u8(3scm) pages.

This procedure is a convenience that sometimes makes it easier to structure code.

Because Scheme's escape procedures have unlimited extent, it is possible to escape from the current continuation but later to resume it. If implementations were permitted to close the port on any escape from the current continuation, then it would be impossible to write portable code using both call-with-current-continuation(3scm) and call-with-port(3scm).

This procedure is identical in R6RS and R7RS, apart from the differences in error handling.

This procedure can raise exceptions with the following condition types:
&assertion (R6RS)
The wrong number of arguments was passed or an argument was outside its domain. In particular, this is raised if procedure does not accept one argument.
R7RS
The assertions described above are errors. Implementations may signal an error, extend the procedure's domain of definition to include such arguments, or fail catastrophically.

call-with-bytevector-output-port(3scm), call-with-string-output-port(3scm), call-with-input-file(3scm), with-input-from-file(3scm)

R6RS, R7RS

This procedure is new in R6RS and later also became new in R7RS.

This page is part of the scheme-manpages project. It includes materials from the RnRS documents. More information can be found at https://weinholt.se/scheme/manpages/.

2022-05-01