At Thu, 16 Mar 2006 10:20:45 +0100, felix winkelmann wrote: > > On 3/15/06, Topher Cyll <[EMAIL PROTECTED]> wrote: > > > > Is there any way to specify generic methods on values instead of class > > in Chicken's tinyclos, like CLOS's (eql value) or Swindle's (singleton > > value) or (name = value) forms? > > TinyCLOS doesn't have anything like this built-in, I'm afraid. > But I think it might be possible with MOP hacking, since classes > can be generated easily.
The attached allows you to write code like the following: (define-generic shortcut-add <value-generic>) (define-method (shortcut-add (x <number>) (y <number>)) (print 'x+y) (+ x y)) (define-method (shortcut-add (x <number>) (zero 0)) (print 'x+zero) x) (define-method (shortcut-add (zero 0) (x <number>)) (print 'zero+x) x) In general, you can specify any non-class value instead of a class. It matches if it's EQUAL? to the argument, and is always more specific than being an INSTANCE-OF? a class. The implementation overrides three methods: COMPUTE-METHOD (to match values), COMPUTE-METHOD-MORE-SPECIFIC? (to determine value matches are more specific), and COMPUTE-APPLY-GENERIC (to remove caching). You could put value-aware caching back in if you want. Unfortunately Chicken currently segfaults when you MAKE an inherited instance of <generic>: $ csi -q #;1> (define x (make-class (list <generic>) (list))) #;2> (make x) $ so <value-generic> above is just an alias for <generic>, and the overrides apply to *all* methods. Once this is fixed, you'll be able to use the extension only for your chosen generics without any slowdown to tinyclos in general. -- Alex
value-generic.scm
Description: Binary data
_______________________________________________ Chicken-users mailing list Chicken-users@nongnu.org http://lists.nongnu.org/mailman/listinfo/chicken-users