[racket] About closing ports

From: Matthew Flatt (mflatt at cs.utah.edu)
Date: Fri Aug 22 15:33:11 EDT 2014

Sorry for the long delay here.

There's no particular reason for the absence of `close-port`, as far as
I know --- just that `close-input-port` and `close-output-port` were
always there.

It's possible for a value to represent both an input port and an output
port, so if that matters at all, you might want

 (define (close-port p)
   (when (input-port? p) (close-input-port p))
   (when (output-port? p) (close-output-port p)))


At Sat, 9 Aug 2014 23:26:25 -0700, Jack Firth wrote:
> Is there any particular reason there's no general close-port function that
> works on both input ports and output ports? Would an implementation along
> the lines of ((if (input-port? p) close-input-port close-output-port) p)
> have problems that make this unreasonable? The only reason I ask is because
> I was writing a macro let-ports that works like let (but only for ports)
> that automatically closes the ports it defines. My naive implementations of
> these are:
> 
> (define (close-port p)
>   ((if (input-port? p) close-input-port close-output-port) p))
> 
> (define-syntax-rule (let-ports ([port port-expr] ...) body ...)
>   (let ([port port-expr] ...)
>     body ...
>     (close-port port) ...))
> ____________________
>   Racket Users list:
>   http://lists.racket-lang.org/users

Posted on the users mailing list.