Anonymous user
Polymorphic copy: Difference between revisions
Factor
(Added C#.) |
(Factor) |
||
Line 570:
let s2 = s.Clone() // the static type of s2 is T, but it "points" to an S
s2.Print() // prints "I'm an S!"</lang>
=={{header|Factor}}==
shallow copy is achieved with the "clone" word.
Deep copy is achieved with the serializing words "object>bytes" and "bytes>object".
Pasting the following in a repl:
<lang factor>USING: classes kernel prettyprint serialize ;
TUPLE: A ;
TUPLE: C < A ;
: serial-clone ( obj -- obj' ) object>bytes bytes>object ;
C new
[ clone ]
[ serial-clone ] bi [ class . ] bi@</lang>
outputs:
C
C
=={{header|J}}==
|