Paste number 83027: simple oop and dispatch etc. with structs

Paste number 83027: simple oop and dispatch etc. with structs
Pasted by: lnostdal
When:2 years, 7 months ago
Share:Tweet this! | http://paste.lisp.org/+1S2B
Channel:None
Paste contents:
Raw Source | XML | Display As
SW-MVC> (defstruct super 
          (x)
          (y))
SUPER
SW-MVC> (defstruct (sub (:include super))
          (z))
SUB
SW-MVC> (defmethod test ((obj super))
          (format t "X: ~A, Y: ~A~%" (super-x obj) (super-y obj)))
#<STANDARD-METHOD TEST (SUPER) {ADFEC41}>
SW-MVC> (defmethod test ((obj sub))
          (format t "X: ~A, Y: ~A, Z: ~A~%" 
                  (super-x obj) (super-y obj) ;; SUB-X and SUB-Y would also work here.
                  (sub-z obj)))                   ;; But here we must use SUB-Z.
#<STANDARD-METHOD TEST (SUB) {BF71C01}>
SW-MVC> (test (make-super :x 1 :y 2))
X: 1, Y: 2
NIL
SW-MVC> (test (make-sub :x 1 :y 2 :z 3))
X: 1, Y: 2, Z: 3
NIL
SW-MVC> 

This paste has no annotations.

Colorize as:
Show Line Numbers

Lisppaste pastes can be made by anyone at any time. Imagine a fearsomely comprehensive disclaimer of liability. Now fear, comprehensively.