Skip to content

Instantly share code, notes, and snippets.

@sjolsen
Created March 12, 2024 21:14
Show Gist options
  • Save sjolsen/297fe87d56ed3fef9a3124a7678ee310 to your computer and use it in GitHub Desktop.
Save sjolsen/297fe87d56ed3fef9a3124a7678ee310 to your computer and use it in GitHub Desktop.
CLOS classes that can be constructed by calling them
(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