Created
March 3, 2016 11:49
-
-
Save rigidus/2fd788c8b6d72634a082 to your computer and use it in GitHub Desktop.
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
(defun direct-slot-defn->initarg (slot-defn) | |
(list :name (slot-definition-name slot-defn) | |
:readers (slot-definition-readers slot-defn) | |
:writers (slot-definition-writers slot-defn) | |
:initform (slot-definition-initform slot-defn) | |
:initargs (slot-definition-initargs slot-defn) | |
:initfunction (slot-definition-initfunction slot-defn))) | |
(defun add-slot-to-class (class name &key (initform nil) accessors readers writers initargs (initfunction (constantly nil))) | |
(check-type class symbol) | |
(let ((new-slots (list (list :name name | |
:readers (union accessors readers) | |
:writers (union writers | |
(mapcar #'(lambda (x) | |
(list 'setf x)) | |
accessors) | |
:test #'equal) | |
:initform initform | |
:initargs initargs | |
:initfunction initfunction)))) | |
(dolist (slot-defn (class-direct-slots (find-class class))) | |
(push (direct-slot-defn->initarg slot-defn) new-slots)) | |
(ensure-class class :direct-slots new-slots))) | |
(defclass foo () | |
((bar :accessor bar :initform "zzzzzz") | |
(baz :accessor baz :initform "zzzzzz"))) | |
(defclass foo () | |
((bar :accessor bar :initform "zzzzzz") | |
(baz :accessor baz :initform "zzzzzz"))) | |
(defmethod slot-missing (class (instance foo) slot-name operation &optional (new-value "defailt value")) | |
(declare (ignorable class)) | |
(print (list class instance slot-name operation new-value)) | |
;; (err 'zz) | |
(add-slot-to-class (class-name class) slot-name) | |
(setf (slot-value instance slot-name) new-value)) | |
(defparameter *foo* (make-instance 'foo)) | |
(setf (slot-value *foo* 'bar) "the-bar") | |
(setf (slot-value *foo* 't2) "zzz") | |
(defparameter *foo2* (make-instance 'foo)) | |
(slot-value *foo2* 't5) | |
(slot-value *foo2* 'bar) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment