close-port(3scm)
β¨ 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
- nixpkgs unstable: scheme-manpages 0-unstable-2024-02-11
- π the page's license: MIT
- Copyright (c) 2021 Gwen Weinholt
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms