define-type and "type-id" access
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? I tried browsing through _nonstd.scm but could not figure out everything that the define-type macro defines. Is there a way to see all the constructors, getters, setters, etc. that a macro would define? Thanks, Joel -- OpenPoker: The Linux of poker software http://wagerlabs.com/forums
Afficher les réponses par date
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
Thank you Marc!
(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.
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? Still, this is a big leg up as I was just wondering yesterday whether Gambit had any hash table functions. Would I implement one based on an associative list? Thanks, Joel -- OpenPoker: The Linux of poker software http://wagerlabs.com/forums
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
participants (2)
-
Joel Reymont -
Marc Feeley