NextPrevUpTopContentsIndex

defclass

Macro
Summary

Remains as defined in ANSI Common Lisp, but extra control over parsing of class options and slot options, optimization of slot access, and checking of initargs, is provided.

Package

common-lisp

Description

The macro defclass is as defined in the ANSI standard with the following extensions.

For extra class options, you may need to define the way these are parsed at defclass macroexpansion time. See process-a-class-option for details.

For non-standard slot options, you may need to define the way these are parsed at defclass macroexpansion time. See process-a-slot-option for details.

By default, standard slot accessors are optimized such that they do not call slot-value-using-class. This optimization can be switched off using the :optimize-slot-access class option.

To add valid initialization arguments for the class, use the class option :extra-initargs . The argument passed via this option is evaluated, and should return a list of extra initialization arguments for the class. make-instance will treat these as valid when checking its arguments.

Compatibility Note

When a class is redefined, its extra initargs are always reset.

In early versions of LispWorks 4.3, extra initargs were not reset when a class was redefined without specifying extra initargs.

Example
CL-USER 116 > (compile '(defclass foo () 
                          ((a :type fixnum 
                              :initarg :a 
                              :reader foo-a))))
NIL
 
CL-USER 117 > (compile '(defclass bar () 
                          ((a :type fixnum 
                              :initarg :a 
                              :reader bar-a)) 
                          (:optimize-slot-access nil)))
NIL
 
CL-USER 118 > (setf *foo* 
                    (make-instance 'foo :a 42) 
                    *bar* (make-instance 'bar :a 99))
#<BAR 20666FDC>
 
CL-USER 119 > (progn 
                (time (dotimes (i 1000000) 
                        (foo-a *foo*))) 
                (time (dotimes (i 1000000)
                        (bar-a *bar*))))
Timing the evaluation of (DOTIMES (I 1000000) (FOO-A *FOO*))
 
user time    =      1.041
system time  =      0.000
Elapsed time =   0:00:01
Allocation   = 2208 bytes standard / 11001760 bytes conses
0 Page faults
Timing the evaluation of (DOTIMES (I 1000000) (BAR-A *BAR*))
 
user time    =      1.422
system time  =      0.000
Elapsed time =   0:00:01
Allocation   = 3456 bytes standard / 11002948 bytes conses
0 Page faults
NIL

This session illustrates the :extra-intargs class option:

CL-USER 46 > (defclass a () ()
               (:extra-initargs '(:a-initarg)))
#<STANDARD-CLASS A 21C2E4FC>
 
CL-USER 47 > (defclass b (a) ()
               (:extra-initargs '(:b-initarg)))
#<STANDARD-CLASS B 2068573C>
 
CL-USER 48 > (defclass c (a) ())
#<STANDARD-CLASS C 22829D44>
 
CL-USER 49 > (make-instance 'b :a-initarg "A" :b-initarg "B")
#<B 2068BCE4>
 
CL-USER 50 > (make-instance 'c :a-initarg "A" :b-initarg "B")
 
Error: MAKE-INSTANCE is called with unknown keyword :B-INITARG among the arguments (C :A-INITARG "A" :B-INITARG "B") which is not one of (:A-INITARG).
  1 (continue) Ignore the keyword :B-INITARG
  2 (abort) Return to level 0.
  3 Return to top loop level 0.
 
Type :b for backtrace, :c <option number> to proceed,  or :? for other options
 
CL-USER 51 : 1 > 
See also

process-a-class-option
process-a-slot-option


LispWorks Reference Manual - 23 Jul 2004

NextPrevUpTopContentsIndex