[gambit-list] define-type and "type-id" access

Marc Feeley feeley at IRO.UMontreal.CA
Tue Dec 7 22:56:28 EST 2004


> Is there a way to get the type-id of a type, though, and register types
> in the hashtable as opposed to objects? Do I have to actually create an
> instance of each object to get their type-ids?

You need to specify a type-exhibitor in the type definition.  In the
following example the type definition will create a function called
"person-type" that returns the type-descriptor of the person type.

> (define-type person
    id: 4b587cfe-f6fc-41cf-953b-f5743fb219ce
    type-exhibitor: person-type
    name
    birthdate)
> (define p (make-person "foo" 2004))
> (##structure-type p)
#<type #2 person>
> (person-type)
#<type #2 person>

If you want to see what a define-type expands into, just use
"pp" like this:

> (pp (lambda () (define-type point x y) 999))
(lambda ()
  (letrec ((##type-2-point
            (##structure
             ##type-type
             (##make-uninterned-symbol "##type-2-point")
             'point
             8
             #f
             '#(x 0 #f y 0 #f)
             '#(1 2)))
           (make-point (lambda (p1 p2) (##structure ##type-2-point p1 p2)))
           (point? (lambda (obj)
                     (##structure-direct-instance-of?
                      obj
                      (##type-id ##type-2-point))))
           (point-x (lambda (obj)
                      (##structure-ref obj 1 ##type-2-point point-x)))
           (point-x-set!
            (lambda (obj val)
              (##structure-set! obj val 1 ##type-2-point point-x-set!)))
           (point-y (lambda (obj)
                      (##structure-ref obj 2 ##type-2-point point-y)))
           (point-y-set!
            (lambda (obj val)
              (##structure-set! obj val 2 ##type-2-point point-y-set!))))
    999))

Marc


More information about the Gambit-list mailing list