call-with-port(3scm)
π call-with-port π
okay bestie, welcome to the Scheme Programmer's Manual, section 3scm β this little icon is all about call-with-port, and it's giving βclean up after yourselfβ energy. The way that it handles ports? Obsessed. π«Ά
πΈ NAME πΈ
call-with-port β call a procedure with a port, closing it afterwards π
π LIBRARY π
Import situation, no cap:
(import (rnrs)) ;R6RS
(import (rnrs io ports)) ;R6RS
(import (scheme base)) ;R7RS
πͺ SYNOPSIS πͺ
(call-with-port port proc)
π DESCRIPTION π
So call-with-port literally calls proc with port as an argument. That's the main character move. β¨
If proc returns, port is closed automatically and the values returned by proc are returned. Slay, automatic cleanup. π
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. That βexcept perhapsβ is doing a lot of heavy lifting, so don't go assuming, bestie. π
π RETURN VALUES π
Returns whatever values proc returns. Literally just passes them through. π«Ά
π EXAMPLES π
Here's the example, exactly:
(call-with-port (open-input-file "config.scm") read)
There are also examples in the put-bytevector(3scm) and put-u8(3scm) pages. Go look, iykyk. π
π οΈ APPLICATION USAGE π οΈ
This procedure is a convenience that sometimes makes it easier to structure code. Not mandatory, just a cute lil helper. π
π§ RATIONALE π§
Okay so here's the lore: 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). That's the whole reason, bestie. π€―
π§© COMPATIBILITY π§©
This procedure is identical in R6RS and R7RS, apart from the differences in error handling. Same vibe, different error drama. π¦
π¨ ERRORS π¨
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. Yes, βfail catastrophicallyβ is the actual phrase, so... yeah. π
π SEE ALSO π
call-with-bytevector-output-port(3scm)call-with-string-output-port(3scm)call-with-input-file(3scm)with-input-from-file(3scm)
π STANDARDS π
R6RS, R7RS. Both. Periodt. β¨
π°οΈ HISTORY π°οΈ
This procedure is new in R6RS and later also became new in R7RS. Glow-up timeline, honestly. π
π AUTHORS π
This page is part of the scheme-manpages project. It includes materials from the RnRS documents. More information can be found at https://github.com/schemedoc/manpages/. π«Ά
mwah π this cutie is the call-with-port(3scm) page, dated 2022-05-01 β source/version and date, right there, no cap. π
π¦ where this page comes from
- nixpkgs unstable: scheme-manpages 0-unstable-2024-02-11
- π the page's license: MIT
- Copyright (c) 2022 Gwen Weinholt
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms