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: Ludovic Courtès
Subject: Re: What should the constructor for a record look like?
Date: Mon, 27 Aug 2018 14:22:38 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/26.1 (gnu/linux)

Hi,

HiPhish <address@hidden> 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’.




reply via email to

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