RosettaCodeData/Task/Polymorphic-copy/TXR/polymorphic-copy.txr

26 lines
618 B
Plaintext

(defstruct base ()
(:method identify (self) (put-line "base")))
(defstruct derived (base)
(:method identify (self) (put-line "derived")))
(defstruct poly ()
obj
(:method deep-copy (self)
(let ((c (copy self))) ;; make copy of s
(upd c.obj copy) ;; copy self's obj
c))) ;; return c
;; Test
(let* ((b (new base))
(d (new derived))
(p (new poly obj d)))
b.(identify) ;; prints base
d.(identify) ;; prints derived
(let ((c p.(deep-copy)))
p.obj.(identify) ;; prints derived
(prinl (eq p.obj c.obj)))) ;; prints nil: c.obj is not a ref to p.obj