koans on types and clos
- types are complicated, not sure how to look up documentation for it would they show up in apropos all? - list formatted types - like array vector can specify type of element, rank / dimentions (not sure there's variancy over the element type?) - clos - similar to structure for structured you get separate functions to get slots, use setf to set generalized variables with classes, I can use generic function to create #'make-instance and access slots with (#'slot-value instance 'slot-name) but there could be defined accessor, reader, writer, and :initarg to name slot to have handle to set value in constructor now that's a lot
This commit is contained in:
@@ -18,17 +18,25 @@
|
||||
;; A class definition lists all the slots of every instance.
|
||||
(color speed))
|
||||
|
||||
;; what's the difference with defstruct?
|
||||
(defstruct struct-racecar
|
||||
color speed)
|
||||
(setq my-struct-racecar (make-struct-racecar :color :red :speed 45))
|
||||
(struct-racecar-color my-struct-racecar)
|
||||
|
||||
;; they have generic setters and accessort, ok
|
||||
(define-test defclass
|
||||
;; Class instances are constructed via MAKE-INSTANCE.
|
||||
(let ((car-1 (make-instance 'racecar))
|
||||
(car-2 (make-instance 'racecar)))
|
||||
;; Slot values can be set via SLOT-VALUE.
|
||||
(setf (slot-value car-1 'color) :red)
|
||||
(setf (slot-value car-1 'speed) 220)
|
||||
(setf (slot-value car-2 'color) :blue)
|
||||
(setf (slot-value car-2 'speed) 240)
|
||||
(assert-equal ____ (slot-value car-1 'color))
|
||||
(assert-equal ____ (slot-value car-2 'speed))))
|
||||
;; Class instances are constructed via MAKE-INSTANCE.
|
||||
(let ((car-1 (make-instance 'racecar))
|
||||
(car-2 (make-instance 'racecar)))
|
||||
;; Slot values can be set via SLOT-VALUE.
|
||||
(setf (slot-value car-1 'color) :red)
|
||||
(setf (slot-value car-1 'speed) 220)
|
||||
(setf (slot-value car-2 'color) :blue)
|
||||
(setf (slot-value car-2 'speed) 240)
|
||||
(assert-equal :red (slot-value car-1 'color))
|
||||
(assert-equal 240 (slot-value car-2 'speed))))
|
||||
;; so, using #'slot-value and #'make-instance
|
||||
|
||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||
|
||||
@@ -44,18 +52,28 @@
|
||||
((color :reader color :writer (setf color))
|
||||
(speed :accessor speed)))
|
||||
|
||||
(setq my-ship (make-instance 'spaceship))
|
||||
(setf (color my-ship) :green)
|
||||
(color my-ship)
|
||||
|
||||
;;; Specifying a reader function named COLOR is equivalent to
|
||||
;;; (DEFMETHOD COLOR ((OBJECT SPACECSHIP)) ...)
|
||||
;;; Specifying a writer function named (SETF COLOR) is equivalent to
|
||||
;;; (DEFMETHOD (SETF COLOR) (NEW-VALUE (OBJECT SPACECSHIP)) ...)
|
||||
;;; Specifying an accessor function performs both of the above.
|
||||
|
||||
;; I don't understand what (defmethod (setf color) ...) means
|
||||
;; is that two atom name? wtf
|
||||
;; nope - function-name::= {symbol | (setf symbol)}
|
||||
;; http://www.lispworks.com/documentation/HyperSpec/Body/m_defmet.htm
|
||||
;; still not quite understand it, lots of complicated things, about different forms
|
||||
|
||||
(define-test accessors
|
||||
(let ((ship (make-instance 'spaceship)))
|
||||
(setf (color ship) :orange
|
||||
(speed ship) 1000)
|
||||
(assert-equal ____ (color ship))
|
||||
(assert-equal ____ (speed ship))))
|
||||
(let ((ship (make-instance 'spaceship)))
|
||||
(setf (color ship) :orange
|
||||
(speed ship) 1000)
|
||||
(assert-equal :orange (color ship))
|
||||
(assert-equal 1000 (speed ship))))
|
||||
|
||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||
|
||||
@@ -66,8 +84,10 @@
|
||||
|
||||
(define-test initargs
|
||||
(let ((bike (make-instance 'bike :color :blue :speed 30)))
|
||||
(assert-equal ____ (color bike))
|
||||
(assert-equal ____ (speed bike))))
|
||||
(assert-equal :blue (color bike))
|
||||
(assert-equal 30 (speed bike))))
|
||||
;; oh, so that's for defining initial values in the constructor
|
||||
;; i guess
|
||||
|
||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user