guile-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: What should the constructor for a record look like?


From: Matt Wette
Subject: Re: What should the constructor for a record look like?
Date: Sun, 26 Aug 2018 09:11:42 -0700
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.9.1


On 08/26/2018 06:04 AM, HiPhish wrote:
Hello, it's me again, the guy who wants to implement MessagePack [1] in Guile.
The specification defines a type of "extension" [2], a pair of an 8-bit
integer and a byte array for data. Implementing this type as a record is
obvious, but what should be the name of the constructor?

     (define-record-type ext
       (make-ext type data)
       ext?
       (type ext-type)
       (data ext-data))

Either `make-ext` or just `ext` seem appropriate. I have seen both types of
constructors, what do you guys say? And while I'm on the topic, what about
types? An extension is only valid if the first field (type) is an integer from
-128 to 127 and the data a vector of bytes (integer in the range from 0 to
255). How do I enforce this invariant without static typing?

[1] https://msgpack.org/
[2] https://github.com/msgpack/msgpack/blob/master/spec.md#ext-format-family

I like make-ext vs ext.

I assume you are using bytevectors for the data.  You could "enforce" that type 
in
the record by using a separate definition of `ext?'.

(define-record-type ext
  (make-ext type data)
  ext-record?
  (type ext-type)
  (data ext-data))

(define (ext? obj)
  (and (ext-record? obj)
       (integer? (ext-type obj))
       (bytevector? (ext-data obj)))





reply via email to

[Prev in Thread] Current Thread [Next in Thread]