23 Generics
(require unstable/generics) |
| ||||||||||||||||||||||||||||||||||||||||
| ||||||||||||||||||||||||||||||||||||||||
|
Defines prop:name as a structure type property. Structure types implementing this generic group should have this property where the value is a vector with one element per method where each value is either #f or a procedure with the same arity as specified by kw-formals*. (kw-formals* is similar to the kw-formals used by lambda, except no expression is given for optional arguments.) The arity of each method is checked by the guard on the structure type property.
Defines name? as a predicate identifying instances of structure types that implement this generic group.
Defines each method as a generic procedure that calls the corresponding method on values where name? is true. Each method must have a required by-position argument that is free-identifier=? to name. This argument is used in the generic definition to locate the specialization.
| ||||||||||||
|
(define-generics (name prop:name name?) |
[method . kw-formals*] |
...) |
where prop:name and name? are created with the lexical context of name.
(define-methods name definition ...) | ||||||
|
Expands to a value usable as the property value for the structure type property of the name generic group.
If the definitions define the methods of name, then they are used in the property value.
If any method of name is not defined, then #f is used to signify that the structure type does not implement the particular method.
Allows define/generic to appear in definition ....
(define/generic local-name method-name) | ||||||||||||
|
Syntactically an error when used outside define-methods.
Examples: | |||||||||||||
| |||||||||||||
| |||||||||||||
| |||||||||||||
> (define x (make-num 10)) | |||||||||||||
> (gen-print x) | |||||||||||||
Num: 10 | |||||||||||||
> (gen-port-print (current-output-port) x) | |||||||||||||
Num: 10 | |||||||||||||
> (gen-print* x #:width 100 #:height 90) | |||||||||||||
Num (100x90): 10 | |||||||||||||
> (define y (make-bool #t)) | |||||||||||||
> (gen-print y) | |||||||||||||
Bool: Yes | |||||||||||||
> (gen-port-print (current-output-port) y) | |||||||||||||
Bool: Yes | |||||||||||||
> (gen-print* y #:width 100 #:height 90) | |||||||||||||
Bool (100x90): Yes |