unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
* What should the constructor for a record look like?
@ 2018-08-26 13:04 HiPhish
  2018-08-26 16:11 ` Matt Wette
  2018-08-27 12:22 ` Ludovic Courtès
  0 siblings, 2 replies; 3+ messages in thread
From: HiPhish @ 2018-08-26 13:04 UTC (permalink / raw)
  To: guile-user

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





^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: What should the constructor for a record look like?
  2018-08-26 13:04 What should the constructor for a record look like? HiPhish
@ 2018-08-26 16:11 ` Matt Wette
  2018-08-27 12:22 ` Ludovic Courtès
  1 sibling, 0 replies; 3+ messages in thread
From: Matt Wette @ 2018-08-26 16:11 UTC (permalink / raw)
  To: guile-user


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)))





^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: What should the constructor for a record look like?
  2018-08-26 13:04 What should the constructor for a record look like? HiPhish
  2018-08-26 16:11 ` Matt Wette
@ 2018-08-27 12:22 ` Ludovic Courtès
  1 sibling, 0 replies; 3+ messages in thread
From: Ludovic Courtès @ 2018-08-27 12:22 UTC (permalink / raw)
  To: guile-user

Hi,

HiPhish <hiphish@posteo.de> skribis:

> 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.

For immutable records (which should be the norm :-)), I often leave out
‘make-’ nowadays.  (The analogy is ‘string’ vs. ‘make-string’, for
instance.)

> I have seen both types of constructors, what do you guys say? And

guys and gals :-)

> 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?

You could enforce it by not exporting the raw record constructor, and
instead exporting a procedure that performs all the necessary checks:

  (define-record-type <ext>
    (%ext type data)         ;private
    ext?
    …)

  (define (ext type data)    ;public
    (assert-valid-arguments type data)
    (%ext type data))

HTH!

Ludo’.




^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2018-08-27 12:22 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2018-08-26 13:04 What should the constructor for a record look like? HiPhish
2018-08-26 16:11 ` Matt Wette
2018-08-27 12:22 ` Ludovic Courtès

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).