[plt-scheme] Re: swindle generic functions top-level and scope

From: Brad (nottysymbaz at yahoo.ca)
Date: Mon Apr 23 10:14:26 EDT 2007

On Apr 22, 9:06 pm, Eli Barzilay <e... at barzilay.org> wrote:
> The stuff that you're doing below is unrelated to the MOP.
> The main problem that you're running against is that `defmethod' is
> strange.  Sometimes it defines a new generic with a method that
> populates it (ie, a `defgeneric' + `add-method'), and sometimes it's
> adding a method to an existing generic.  To avoid confusion, the best
> thing you can do is keep all generics at the top-level, and use
> `add-method' in nested scopes.  Local generic definitions are
> problematic and you probably don't want them anyway: the cost of
> creating a generic is much more than creating a closure (which can
> often be lifted so there is no cost).

Thank you for your insight. I've never used CL before and am just
beginning to explore Swindle. I think I'm starting to get my head
around generics. So far I really like it. For my tastes it's a much
more natural and consise way to express OO than any of the
alternatives I've seen so far.

> > (defclass <foo> ())
> > (defmethod (bar (self <foo>))
> >   (printf "noo bar noo foo~n"))
> > (define foo-2 (make <foo>))
> > (bar foo-1)   ;; -> bar (an origial foo -- can't make these anymore)
> > (bar foo-2)   ;; -> noo bar noo foo (this is a noo foo)
> This is another problem, not specific to Swindle, or even to
> MzScheme.  You've created a new class that has the same name, and
> added a method with the new class as a specializer to `bar'.  The
> thing is that `foo-1' is still an instance of the old `foo'.  Common
> Lisp is trying to "sort of" solve this by making the second class
> definition modify the class object, so all instances still maintain a
> link to the same class and will change themselves when used again.
> The moral of this is that you should be careful with re-defining
> classes -- it's best to throw away all old instances.  You can see the
> same problem with plain closures:

I don't think this is a problem. I would rather have this than
accidentally overwrite an existing class like <string> for example.


Posted on the users mailing list.