Polymorphic copy: Difference between revisions

CL (for structures)
(Added Slate implementation)
(CL (for structures))
Line 114:
}
</lang>
 
=={{header|Common Lisp}}==
===With Structures===
With structures, <code>[http://www.lispworks.com/documentation/HyperSpec/Body/f_cp_stu.htm copy-structure]</code> performs the right kind of copy. The object and its copy are compared under <code>eq</code>, <code>eql</code>, <code>equal</code>, and <code>equalp</code> to demonstrate that "The objective is to create an exact copy of such polymorphic object (not to create a reference, nor a pointer to)."
 
<lang lisp>(defstruct super foo)
 
(defstruct (sub (:include super)) bar)
 
(defgeneric frob (thing))
 
(defmethod frob ((super super))
(format t "~&Super has foo = ~w." (super-foo super)))
 
(defmethod frob ((sub sub))
(format t "~&Sub has foo = ~w, bar = ~w."
(sub-foo sub) (sub-bar sub)))</lang>
 
<pre>> (let* ((sub1 (make-sub :foo 'foo :bar 'bar))
(sub2 (copy-structure sub1)))
(frob sub1)
(frob sub2)
(format t "~&eq: ~w; eql: ~w; equal: ~w; equalp: ~w"
(eq sub1 sub2) (eql sub1 sub2)
(equal sub1 sub2) (equalp sub1 sub2)))
Sub has foo = FOO, bar = BAR.
Sub has foo = FOO, bar = BAR.
eq: NIL; eql: NIL; equal: NIL; equalp: T
NIL</pre>
 
=={{header|D}}==
Anonymous user