Created
March 12, 2024 21:14
-
-
Save sjolsen/297fe87d56ed3fef9a3124a7678ee310 to your computer and use it in GitHub Desktop.
CLOS classes that can be constructed by calling them
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defclass callable-class (standard-class function) | |
() | |
(:metaclass sb-mop:funcallable-standard-class)) | |
(defmethod sb-mop:validate-superclass ((class callable-class) superclass) | |
(or (typep superclass 'callable-class) | |
(typep superclass 'standard-class))) | |
(defmethod initialize-instance :after ((class callable-class) &rest rest &key &allow-other-keys) | |
(declare (ignore rest)) | |
(sb-mop:set-funcallable-instance-function | |
class | |
#'(lambda (&rest args) (apply #'make-instance class args))) | |
(setf (symbol-function (class-name class)) class)) | |
(defclass foo () | |
((x :initarg :x :accessor foo-x)) | |
(:metaclass callable-class)) | |
(defclass bar (foo) | |
((y :initarg :y :accessor bar-y)) | |
(:metaclass callable-class)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment