[gambit-list] destructuring bind define-type

Marc Feeley feeley at iro.umontreal.ca
Tue Jun 2 10:56:42 EDT 2009


On 2-Jun-09, at 12:24 AM, lowly coder wrote:

> are there any amcros that play well with destructuring bind of  
> define-type's ? I just want to check to see if there's a pre- 
> existing solution before re-inventing my own.
>
> I find myself writing way too much code of the form:
>
> (define-type foo a b)
> (define-type bar c d)
>
> (lambda (x)
>   (let ((c (bar-c x) (d (bar-d x)))
>     (let ((a (foo-a a) (b (foo-b b)) ...

Try this (but beware that the API for the ## procedures may change in  
the future):

(define-macro (def-type . args)
   (##define-type-parser
    'def-type
    #f
    args
    (lambda (name
             flags
             id
             extender
             constructor
             constant-constructor
             predicate
             implementer
             type-exhibitor
             prefix
             fields
             total-fields)
      (let ((field-names (map car fields)))
        `(begin
           (define-type , at args)
           (define (,(##symbol-append 'with- name) obj proc)
             (proc ,@(map (lambda (f)
                            `(,(##symbol-append name '- f) obj))
                          field-names))))))))

(def-type point x y)

(define p (make-point 11 22))

(with-point p (lambda (x y) (pp (list 'x+y= (+ x y)))))


Marc




More information about the Gambit-list mailing list