[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Chicken-users] tinyclos value matching?
From: |
Alex Shinn |
Subject: |
Re: [Chicken-users] tinyclos value matching? |
Date: |
Thu, 16 Mar 2006 21:30:36 -0600 |
User-agent: |
Wanderlust/2.10.1 (Watching The Wheels) SEMI/1.14.6 (Maruoka) FLIM/1.14.6 (Marutamachi) APEL/10.6 Emacs/21.3 (i386-pc-linux-gnu) MULE/5.0 (SAKAKI) |
At Thu, 16 Mar 2006 10:20:45 +0100, felix winkelmann wrote:
>
> On 3/15/06, Topher Cyll <address@hidden> 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