close-port(3scm)

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

✨ close-port β€” bestie, let's close a port ✨

Okay so this is close-port from the Scheme Programmer's Manual (3scm), and honestly? It's giving "tiny procedure, huge main character energy." Let's get into it πŸ’…


🌸 LIBRARY 🌸

Import it from wherever your Scheme heart lives, queen:

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

πŸŽ€ SYNOPSIS πŸŽ€

(close-port port)

πŸ¦‹ DESCRIPTION πŸ¦‹

So close-port literally closes the port. Like, done. Finished. The port is now incapable of delivering or accepting data β€” no more reads, no more writes, that chapter is CLOSED 🫢

If port is an output port, it gets flushed before being closed. Yes bestie, we're not losing your buffered data on the way out, that would be a tragedy 😭

And if the port is already closed? This does nothing. Chill. No error, no drama, no cap.

Also, super important detail: a closed port is still a port. It doesn't stop being a port just because it's closed. It's still an icon, it's just an icon that's done working for the day ✨

πŸ’œ R6RS πŸ’œ

If port is a custom port, then the closer procedure is called before the port is really closed. So like, the custom port gets its little goodbye moment first, then the actual closing happens. Manners, honestly πŸ‘‘


⭐ RETURN VALUES ⭐

Returns unspecified values. It's not here to give you a gift, it's here to do a job πŸ’€


πŸ”₯ EXAMPLES πŸ”₯

(define p (open-output-file "hello.txt"))
(display "Hello, world\n" p)
(close-port p)

Look at that. Open it, write to it, close it. The whole lifecycle. She's a full story arc 🌈


πŸ‘‘ APPLICATION USAGE πŸ‘‘

Ports are often connected to some limited resource, such as a file descriptor. And that means closing ports is genuinely important if you want to avoid resource leaks β€” this is not a drill, bestie 🚨

Some implementations will garbage collect ports, but just know: this is not required. So don't be relying on it, iykyk πŸ™ˆ


🧚 COMPATIBILITY 🧚

This procedure is new in R6RS and R7RS. Fresh-ish. Iconic either way πŸ’…


🌸 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. Basically: you did it wrong, bestie 😭

πŸ’€ 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. Yeah. Catastrophically. That's the actual word. So maybe just pass a real port, okay? 🫢


πŸ“ SEE ALSO πŸ“


🌟 STANDARDS 🌟

R6RS, R7RS. Both. She's certified ✨


πŸ’– 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/. Shoutout to them fr fr 🫢


⚠️ BUGS ⚠️

It is often better to use call-with-port(3scm) or one of the related procedures. Why? Because they make it easier to ensure that a port is closed when an exception is raised. So like, if your code goes sideways mid-write, call-with-port has your back and plain close-port does not. Not me warning you with love πŸ’œ


2021-02-28 β€” that's the date, the page is from the Scheme Programmer's Manual, and this has been your port-closing pep talk. Go close your ports, bestie, your file descriptors will thank you πŸ’…πŸŽ€πŸ¦‹
πŸ“¦ where this page comes from