Binary operations on current-{input,output}-port

6 views
Skip to first unread message

Göran Weinholt

unread,
Aug 23, 2020, 11:35:11 AM8/23/20
to scheme-re...@googlegroups.com
Hello schemers,

I noticed something in R7RS that I find puzzling.

The current input and output ports are textual ports, at least
initially:

| (current-input-port) procedure
| (current-output-port) procedure
| (current-error-port) procedure
| Returns the current default input port, output port, or error port (an
| output port), respectively. […] The initial bindings for these are
| implementation-defined textual ports.

And it is used by default in several of the input procedures:

| 6.13.2. Input
| If port is omitted from any input procedure, it defaults to the value
| returned by (current-input-port). […]

However, this *textual* port is also used for the *binary* input
procedures:

| (read-u8) procedure
| (read-u8 port) procedure
| Returns the next byte available from the binary input port, updating
| the port to point to the following byte. If no more bytes are
| available, an end-of-file object is returned.

Note the (read-u8) entry, which will default to using
(current-input-port) according to the text in 6.13.2.

The entries for (peek-u8), (u8-ready?), (read-bytevector k) and
(read-bytevector! bytevector) are similar.

On the output side there are (write-u8 byte) and (write-bytevector
bytevector), which similarly use (current-output-port).

Is there some specified behavior I'm missing? It seems to me that
(read-u8) etc are straight up errors.

--
Göran Weinholt | https://weinholt.se/
Debian Developer | 73 de SA6CJK

John Cowan

unread,
Aug 23, 2020, 3:25:39 PM8/23/20
to scheme-re...@googlegroups.com
The point is that current-*-port is a parameter you can rebind, and you would probably do so if you were doing a lot of binary I/O.  They are initially bound to std* and assumed to be character ports because backward compatibility.

--
You received this message because you are subscribed to the Google Groups "scheme-reports-wg1" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scheme-reports-...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/scheme-reports-wg1/87blj1o0k9.fsf%40teapot.weinholt.se.

Göran Weinholt

unread,
Aug 24, 2020, 2:34:09 AM8/24/20
to scheme-re...@googlegroups.com
Hello John,

What you describe is a plausible scenario where the specified
semantics work. But a program would have to be careful to not
accidentally use them as text ports during such a rebinding, which
would be an error.

I'm not convinced that it's a useful footgun to have in a standard,
but that is probably due to my personal preferences more than anything
else, so I will not push to change this.

Thanks for the clarification!

/Göran

On Sun, Aug 23, 2020 at 03:25:27PM -0400, John Cowan wrote:
> The point is that current-*-port is a parameter you can rebind, and you
> would probably do so if you were doing a lot of binary I/O.  They are
> initially bound to std* and assumed to be character ports because backward
> compatibility.
> On Sun, Aug 23, 2020 at 11:35 AM Göran Weinholt <[1]go...@weinholt.se>
> Göran Weinholt   | [2]https://weinholt.se/
> Debian Developer | 73 de SA6CJK
>
> --
> You received this message because you are subscribed to the Google
> Groups "scheme-reports-wg1" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to [3]scheme-reports-...@googlegroups.com.
> To view this discussion on the web visit
> [4]https://groups.google.com/d/msgid/scheme-reports-wg1/87blj1o0k9.fsf%40teapot.weinholt.se.
>
> --
> You received this message because you are subscribed to the Google Groups
> "scheme-reports-wg1" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [5]scheme-reports-...@googlegroups.com.
> To view this discussion on the web visit
> [6]https://groups.google.com/d/msgid/scheme-reports-wg1/CAD2gp_T-U%3Dc30Am7W8bYZ4Vj56x9Ya7UK%3Du-6N%2B44ywzj1hbhQ%40mail.gmail.com.
>
> References
>
> Visible links
> 1. mailto:go...@weinholt.se
> 2. https://weinholt.se/
> 3. mailto:scheme-reports-wg1%2Bunsu...@googlegroups.com
> 4. https://groups.google.com/d/msgid/scheme-reports-wg1/87blj1o0k9.fsf%40teapot.weinholt.se
> 5. mailto:scheme-reports-...@googlegroups.com
> 6. https://groups.google.com/d/msgid/scheme-reports-wg1/CAD2gp_T-U%3Dc30Am7W8bYZ4Vj56x9Ya7UK%3Du-6N%2B44ywzj1hbhQ%40mail.gmail.com?utm_medium=email&utm_source=footer
Reply all
Reply to author
Forward
0 new messages