[plt-scheme] struct-type-info

From: Jens Axel Søgaard (jensaxel at soegaard.net)
Date: Fri Dec 1 11:10:50 EST 2006

Matt Jadud skrev:
> Hi all,
> 
> Given a structure instance 'a-foo':
> 
> (define-struct foo (a b))
> (define a-foo (make-foo 3 5))
> 
> Is there any way, given an instance of a foo (a 'make-foo') to get at 
> the struct-type-info? Or, do I need to define my own structure using 
> make-structure-type and hide that information somewhere?
> 
> Given a structure of any type, I'd like to generically walk the fields. 
> However, given a structure instance, I'm not clear how I find out what 
> its accessor method should be...

Walk fields? Perhaps you can use this?

; map-slots! (alpha -> beta) struct -> struct
;  replace each slot sl in the struct s with (f sl)
;  the fields of a super structure is only replaced if they are
;  accessible by the inspector

(define (map-slots! f s)
   (define (map-slot/type! f s t)
     (let-values ([(name init-k auto-k s-ref s-set! imm super skipped)
                   (struct-type-info t)])
       (do ([i 0 (add1 i)])
         ((= i (+ init-k auto-k)))
         (s-set! s i (f (s-ref s i))))))

   (define (super-types s)
     (define (super-types-from t)
       (let-values ([(name init-k auto-k s-ref s-set! imm super skipped)
                     (struct-type-info t)])
         (if super
             (cons super (super-types-from super))
             empty)))
     (let-values ([(type skipped?) (struct-info s)])
       (cons type (super-types-from type))))

   (for-each (lambda (t)
               (map-slot/type! f s t))
             (reverse (super-types s))))


-- 
Jens Axel Søgaard



Posted on the users mailing list.