[gambit-list] define-type and "type-id" access
Marc Feeley
feeley at IRO.UMontreal.CA
Tue Dec 7 16:34:20 EST 2004
> Folks,
>
> I define a type like this:
>
> (define-type person
> id: 83C73CCE-46D5-11D9-9982-00039301BA52
> name
> birthdate
> )
>
> I'm looking to have a case statement where I evaluate the passed object
> and take actions depending on its id. I would need to compare object.id
> to type.id, though. What statement will give me the object.id and the type.id?
Typically you would do this:
(cond ((person? x)
...handle a person)
((date? x)
...handle a date)
(else
...fail))
An alternative is to use these undocumented functions:
(define (id obj) (##type-id (##structure-type obj)))
(id (make-person 1 2)) => ##type-2-83C73CCE-46D5-11D9-9982-00039301BA52
(##symbol-hash (id (make-person 1 2))) => 189034871
With these functions you can implement a hash-table that is accessed
using a structure's type.
I'm currently designing a generic hashtable datatype that will
simplify all of this.
Marc
More information about the Gambit-list
mailing list