Re: bytestring isn't a datatype, right? Marc Nieper-Wißkirchen 08 Oct 2020 13:23 UTC

Am Do., 8. Okt. 2020 um 15:13 Uhr schrieb Shiro Kawai <xxxxxx@gmail.com>:

> On Thu, Oct 8, 2020 at 3:07 AM Lassi Kortela <xxxxxx@lassi.io> wrote:
>>
>>
>> (bytestring<? bytevector1 bytevector2) could just as well be named
>> `bytevector<?` unless I misunderstood something.
>
>
> I feel it natural that bytevector<? behaves as (<? (make-default-comparator) bvec1 bvec2) --- that is, the default comparison order.
> It differs from the proposed bytestring<? ordering.

What is the actual use case for lexicographic comparison of vectors code points?

If we have to deal with different encodings, the best (typesafe) way
would be to define an actual new datatype called "bytestring", which
bundles a bytevector together with an opaque (?) encoding marker. We
can then have type-safe procedures

(bytestring->string bs)

and

(string->bytestring encoding str)

Comparison (bytestring<? a b) would then be equivalent to (string<?
(bytestring->string a) (bytestring->string b)).

A procedure read-bytestring will try to use the native encoding of the
textual port. A procedure write-bytestring will signal an error if the
port's encoding is not the same as the one of the bytestring.