call-with-port(3scm)

πŸ“¦ version 0-unstable-2024-02, in nixpkgs unstable

πŸ’– 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 πŸ‘€

πŸ“œ 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