chicken-users
[Top][All Lists]
Advanced

[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: Wed, 06 Apr 2005 15:50:28 +0000

On 04/06/05 01:47:57, felix winkelmann wrote:
>
> I also think there might be a problem with define-macro... With the
above patch to tinyclos,
> the following code gives somewhat strange results:
>
> #;2> (define-method (foo (a <top>) b) (print "two " a " " b))
> #;3> (define-method (foo (a <top>)) (print "one " a))
> #;4> (foo 3 2)
> Error: bad argument count - received 3 but expected 2
> #;4> (foo 3)
> one 3
> "one "
> #;5> (define-method (foo (a <top>) (b <top>)) (print "two other " a " "
b))
> #;6> (foo 3 2)
> two other 3 2
> "two other "
>
> What is going on here is that when expanding the first define-method,
define-method is
> passing the list of specializers to (add-global-method) as '(<top>).
This is because
> of lines 482 in chicken-highlevel-macros.scm:
>     (if (or (not (pair? args))
>             (and (not (pair? (car args)))
>                  (not (scan (cdr args))) ) )
> Since define-method stops generating the list of specializers when it
finds no more
> pairs left in the list of arguments, b's type (which is <top>) is never
getting added
> to the list. It makes for a wierd case where wrapping b inside a (b
<top>) works
> whereas it doesn't if b is just left open.
>
> Thus, during add-method, since the list of specializers for the first
method is equal
> to the list for the second, the second define-method is replacing the
first.
>

But is this so weird? A non-specialized argument ("b" in the first
define-method) must be an instance of something in the end...


No, it doesn't. The way define-method is written, in the first define method the list of specializers is (<top>). That is, define method passes a list of specializers that is one shorter than the actual argument lists. Try something like the following:

(define (printspecs var sym specs function)
  (print specs))

(define <top> 'top)

(set! ##tinyclos#add-global-method printspecs)

(define-method (foo (a <top>) b) (print "hey"))
(define-method (foo (a <top>) (b <top>)) (print "yo"))
(define-method (foo (a <top>) b (c <top>)) (print "2"))

You can see that the first one prints
(top)
and the second prints
(top top)

The reason is because define-method stops processing the list of arguments when it checks and sees there are no more pairs left in the list. (See the scan function).

John




reply via email to

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