[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Chicken-users] tinyclos and variable argument functions

From: John Lenz
Subject: Re: [Chicken-users] tinyclos and variable argument functions
Date: Tue, 05 Apr 2005 06:22:46 +0000

On 04/04/05 01:15:03, felix winkelmann wrote:
> Hi, I am trying to do something like this, which works in other
> languages (like GOOPS for guile)
> (require 'tinyclos)
> (define-method (foo (a <top>) (b <top>)) (print "two"))
> (define-method (foo (a <top>)) (print "one"))
> (foo 3)
> -> prints "one"
> (foo 1 2)
> Error: bad argument count - received 3 but expected 2
>  If I reverse the way foo is defined, by defining the one before the
> two, then the (foo 3) will give a bad argument count. I use <top> in > this example, but it is the same for other classes as well.
> Is this just a limitation of tinyclos or is it a bug in tinyclos?

It is a limitation. The second definition of foo doesn't have a
congruent  argument lists. I guess this is to allow dynamic redefinition,
but I'd have to dig deeper into the tinyclos implementation to
find out.

> If it is  a limitation, what do you think is the best way to get around
> it? Write  a  macro that looks similar to define-method, but creates a
> new generic for each argument length and stores them all in a vector? > Then return a function that first checks the argument length and looks > up the right generic to apply?

Yes, that would be a solution. But perhaps I can fix tinyclos...

I've taken a closer look at tinyclos.scm, and I have got the simple example above working with the following patch to tinyclos.scm. Note that this slightly changes the semantics of how generic functions work, because something like

(define foo (make-generic "foo"))

(add-method foo
  (make-method (list <top> <top>)
    (lambda (call-next-method . args) (print "two " args) )))

(add-method foo
  (make-method (list <top>)
    (lambda (call-next-method . args) (print "one " args) )))

(foo 3 2)
In the current code, it will print "one (3 2)", but with my changes it prints correctly "two (3 2)"... this only is the case if variable argument functions are passed to make-method, otherwise a paramater error resulted and now the method will be correctly executed.

I have not yet updated the method caching to work... If you turn method caching on,
(foo 3 2)
(foo 3)

because the method caching is not taking into account argument length. But this patch is a start and shows the place where tinyclos needs to be changed.


Attachment: tinyclos.diff
Description: Text Data

reply via email to

[Prev in Thread] Current Thread [Next in Thread]