Record types checking Antero Mejr (22 Sep 2024 06:36 UTC)
Re: Record types checking Daphne Preston-Kendal (22 Sep 2024 10:44 UTC)
Re: Record types checking Artyom Bologov (22 Sep 2024 17:07 UTC)
Re: Record types checking Arthur A. Gleckler (22 Sep 2024 17:53 UTC)
Re: Record types checking Artyom Bologov (22 Sep 2024 18:12 UTC)
Re: Record types checking Artyom Bologov (23 Sep 2024 18:10 UTC)

Record types checking Antero Mejr 22 Sep 2024 06:36 UTC

Would it be helpful to add a define-record-type-checked macro? Since
define-record-type can generate many procedures, it could be tedious to
wrap each of them with checked versions.

Here is a (mostly) portable R7RS implementation built on
define-record-type:
```
(cond-expand
 (gauche) ; Does not work in Gauche due to inlining.
 (else
  (define-syntax set!-record-field-checked
    (syntax-rules ()
      ((_ predicate check)
       (begin))
      ((_ predicate check mutator)
       (let ((x mutator))
         (set! mutator (lambda-checked ((obj predicate) (val check))
                                       (x obj val)))))))
  (define-syntax accessor-checked
    (syntax-rules ()
      ((_ predicate accessor)
       (let ((x accessor))
         (set! accessor (lambda-checked ((obj predicate))
                          (x obj)))))))
  (define-syntax define-record-type-checked
    (syntax-rules ()
      ((_ type
          (constructor (tag check) ...)
          predicate
          (field-tag accessor . more) ...)
       (begin (define-record-type type
                (new-constructor tag ...)
                predicate
                (field-tag accessor . more) ...)
              (define-checked (constructor (tag check) ...)
                (new-constructor tag ...))
              (accessor-checked predicate accessor) ...
              (set!-record-field-checked predicate check . more)
              ...))))))
```