Syntactic sugar for protocol clauses Marc Nieper-Wißkirchen (30 Oct 2022 20:39 UTC)
Re: Syntactic sugar for protocol clauses Marc Nieper-Wißkirchen (31 Oct 2022 10:10 UTC)

Re: Syntactic sugar for protocol clauses Marc Nieper-Wißkirchen 31 Oct 2022 10:10 UTC

PS I just noticed that the syntax of my `construct' macro has to be
amended; the problem is that the rest argument cannot be detected if
it is a pair.  But this does not touch the general idea.

Am So., 30. Okt. 2022 um 21:39 Uhr schrieb Marc Nieper-Wißkirchen
<xxxxxx@nieper-wisskirchen.de>:
>
> SRFI 237 inherits the protocol clause from R6RS.  It maps directly to
> the protocol mechanism of the procedural layer.  For the casual record
> user, however, this makes things look more complicated than they are
> and for such a user, it is not evident that they have to use a clause
> named "protocol" when they want to define a custom constructor.
>
> Thus I propose the addition of a new "constructor" clause that expands
> into a protocol clause.  The syntax of a constructor clause, as I
> suggest, it is
>
> (constructor <expr>)
>
> It expands in the case of base record types to
>
> (protocol
>   (lambda (p)
>     (let-syntax
>         (construct
>          (syntax-rules
>            [(_ x ... . y) (apply x ... y)]))
>       <expr>)))
>
> Here, the identifier `construct' is constructed via (datum->syntax k
> 'construct) where `k' is the define-record-type keyword of the
> definition.
>
> In the case of a child record type, it expands to
>
> (protocol
>   (lambda (n)
>     (let-syntax
>         (construct
>          (syntax-rules
>            [(_ (a ... . b) x ... . y) (apply (apply n a ... b) x ... y)]))
>       <expr>)))
>
> So, instead of
>
> (define-record-type point
>   (fields x y)
>   (protocol
>     (lambda (p)
>       (lambda (x y)
>         (assert (real? x))
>         (assert (real? y))
>         (p x y)))))
>
> one can write
>
> (define-record-type point
>   (fields x y)
>   (constructor
>     (lambda (x y)
>       (assert (real? x))
>       (assert (real? y))
>       (construct x y))))
>
> And instead of
>
> (define-record-type colored-point
>   (parent point)
>   (fields color)
>   (protocol
>     (lambda (n)
>       (lambda (x y c)
>         (assert (color? c))
>         ((n x y) c)))))
>
> one can write
>
> (define-record-type colored-point
>   (parent point)
>   (fields color)
>   (constructor
>     (lambda (x y c)
>       (assert (color? c))
>       (construct (x y) c)))
>
> The name `construct' is open to debate, of course, as is the exact
> syntax of the construct macro.  (It cannot be a function because it
> needs to accept two arbitrarily long argument lists).
>
> Thanks for reading,
>
> Marc