On Thu, 2009-12-24 at 05:31 -0600, Eduardo Cavazos wrote:

> Something I use heavily in the 'box2d-lite' code is a
> 'define-record-type++' macro:
> 
> http://github.com/dharmatech/box2d-lite/blob/master/util/define-record-type.sls
> 
> It expands into a call to the full 'define-record-type' along with some
> additional definitions.
> 
> However, it's cheesy in that it only supports a subset of the syntax of
> the full 'define-record-type', along with some additional syntax.
> 
> Is there a pure R6RS definition of the 'define-record-type' macro
> available somewhere? The definitions I found had implementation specific
> code in them.
> 
> I'd rather base my 'define-record-type' deriviative on a full version.
> I.e. start with the full definition and throw in my extensions.

You don't need to go that far.  Just extract what you need and pass
everything else through:

(define-syntax define-record-type++
  (lambda (stx)
    (syntax-case stx ()
      ((_ name-spec is-type import-type rest ...)
       ;; Invalid name-spec will be detected by define-record-type.
       ;; Invalid is-type or import-type should be detected by
       ;; define-is-type-syntax or define-import-type-syntax.
       ;; Invalid rest will be detected by define-record-type.
       (let loop ((r (syntax (rest ...)))
                  (f #F)
                  (m #F)
                  (a (quote ())))
         (if (null? r)
           (if (and f m)
             (with-syntax (((field-name ...) f)
                           (methods m)
                           ((DRT-clauses ...) (reverse a))
                           (type-name (syntax-case (syntax name-spec) ()
                                        ((x . _) (syntax x))
                                        (_ (syntax name-spec)))))
               (syntax
                (begin
                  (define-record-type name-spec
                    DRT-clauses ...)
                  (define-is-type-syntax is-type
                    type-name
                    (fields field-name ...)
                    methods)
                  (define-import-type-syntax import-type
                    type-name
                    (fields field-name ...)
                    methods))))
             (syntax-violation #F "missing fields and/or methods" stx #F))
           (syntax-case (car r) (fields methods)
             ((fields x ...)
              ;; Multiple fields clauses will be detected by define-record-type.
              (loop (cdr r)
                    (map (lambda (x)
                           (syntax-case x ()
                             ((_ name . _) (syntax name))
                             (_ x)))
                         (syntax (x ...)))
                    m
                    (cons (car r) a)))
             ((methods . _)
              ;; Invalid methods sub-clauses should be detected by
              ;; define-is-type-syntax and/or define-import-type-syntax.
              (if m
                (syntax-violation #F "multiple methods clauses" stx #F)
                (loop (cdr r) f (car r) a)))
             (_
              (loop (cdr r) f m (cons (car r) a))))))))))


I tested that this works:

(define-record-type++ (foo foo-maker is-it-a-foo?)
  is-foo import-foo
  (fields a (mutable b) (immutable c c-get))
  (methods)
  (sealed #T))


-- 
: Derick
----------------------------------------------------------------

Reply via email to