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

Re: Two questions and two comments

Here are some answers to questions you asked some time ago.  Sorry for
the delay.

    Sometimes I find myself holding onto a class object and wanting
    to know the value of one of its class variables.  Does/will CLOS
    provide a way to access class variables other than via class
    instances (sometimes I haven't yet allocated any instances when I
    need the value)?

Try using CLASS-PROTOTYPE to get the cached, prototype instance of a
class.  Like this:

(slot-value (class-prototype *foo-class*) 'some-class-variable)

    Comment: For the sake of uniformity, I would vote that
    initforms of class variables be evaluated just as they are for
    instance variables -- I get temporarily tripped-up every so often
    by the inconsistency in the syntax.

The fact that this doesn't happen is a bug in the current version of

    Second question:  The "add-named-class" function allows us to
    create a class "on-the-fly" without having to use "eval".  Is there
    a similar construct for creating methods at run time (i.e., will it
    be advertised)?  PCL obviously has such a function, since methods
    can be created as a side-effect of calling "add-named-class".  By
    the way, is "add-name-class" part of CLOS, or just a PCL feature?

Yes, there is a function called add-named-method.  I expect both
add-named-class and add-named-method will be part of the CLOS spec.

    Suggestion:  I did some timings on compiled segments of CLOS
    code (on a Symbolics), and found that a slot access is 20 times
    faster if a "with-slots" is declared with :use-accessors = nil than
    if the slot is accessed with the ordinary syntax (and its 30 times
    faster than a "slot-value" form).  So, there is great incentive to
    place "with-slots" declarations everywhere.  I've invented a
    "let-slots" macro which expands to a "let" statement with a
    "with-slots" inside of it, which looks much more attractive than
    having to declare both statements.  My suggestion is to put
    something equivalent to "let-slots" into CLOS.

The magnitude of these performance characteristics is a (mis)feature of
PCL.  This is changing in PCL, for example, the 20 times ratio you cite
will probably be more like 5 in the next release.  What's more, as
implementation-specific versions start appearing, it will go down even
farther.  The real reason to :use-accessors or not is that it means two
different things to use accessors or not.  :use-accessors t means call a
generic function to get/set the value of the slot; a generic function on
which I might define methods by hand.  :use-accessors nil, because it
goes through slot-value, means that there are no generic functions which
could be specialized to change the way the "slot" values are computed in
the body of the with-slots.