find-class
find-class Accessor
Syntax:
find-class symbol &optional errorp environment → class
(setf (find-class symbol &optional errorp environment**)** new-class**)**
Arguments and Values:
symbol—a symbol.
errorp—a generalized boolean. The default is true.
environment – same as the &environment argument to macro expansion functions and is used to distinguish between compile-time and run-time environments. The &environment argument has dynamic extent; the consequences are undefined if the &environment argument is referred to outside the dynamic extent of the macro expansion function.
class—a class object, or nil.
Description:
Returns the class object named by the symbol in the environment. If there is no such class, nil is returned if errorp is false; otherwise, if errorp is true, an error is signaled.
The class associated with a particular symbol can be changed by using setf with find-class; or, if the new class given to setf is nil, the class association is removed (but the class object itself is not affected). The results are undefined if the user attempts to change or remove the class associated with a symbol that is defined as a type specifier in this standard. See Section 4.3.7 (Integrating Types and Classes).
When using setf of find-class, any errorp argument is evaluated for effect, but any values it returns are ignored; the errorp parameter is permitted primarily so that the environment parameter can be used.
The environment might be used to distinguish between a compile-time and a run-time environment.
Exceptional Situations:
If there is no such class and errorp is true, find-class signals an error of type error.
See Also:
defmacro, Section 4.3.7 (Integrating Types and Classes)
Expanded Reference: find-class
Looking Up a Class by Name
find-class returns the class object associated with a given symbol. By default, it signals an error if no class is found.
(find-class 'standard-class)
==> #<STANDARD-CLASS STANDARD-CLASS>
(find-class 'integer)
==> #<BUILT-IN-CLASS INTEGER>
Suppressing the Error
Pass nil as the second argument (errorp) to return nil instead of signaling an error when no class is found.
(find-class 'nonexistent-class nil)
=> NIL
(find-class 'nonexistent-class nil nil)
=> NIL
Using find-class with User-Defined Classes
After defclass, the class is registered and can be looked up by name.
(defclass vehicle ()
((make :initarg :make :accessor vehicle-make)))
(class-name (find-class 'vehicle))
=> VEHICLE
;; You can use the class object with make-instance
(let ((v (make-instance (find-class 'vehicle) :make "Toyota")))
(vehicle-make v))
=> "Toyota"
Setting a Class Name Mapping with setf
You can associate a symbol with a class object using (setf find-class), or remove an association by setting it to nil.
(defclass my-class () ())
;; Create an alias
(setf (find-class 'my-alias) (find-class 'my-class))
(eq (find-class 'my-alias) (find-class 'my-class))
=> T
;; Remove the alias
(setf (find-class 'my-alias) nil)
(find-class 'my-alias nil)
=> NIL
Checking Built-In Types
All standard Common Lisp types that are classes can be looked up with find-class.
(mapcar (lambda (name) (class-name (find-class name)))
'(cons symbol string hash-table))
=> (CONS SYMBOL STRING HASH-TABLE)