Polymorphic copy: Difference between revisions

no edit summary
m (→‎{{header|Forth}}: - Layout issue)
No edit summary
Line 781:
i2c: {6 one} / I'm an s! / main.s
i3c: {7 0x0} / I'm a t! / main.r
</pre>
 
=={{header|Icon}} and {{header|Unicon}}==
 
Icon and Unicon do no compile-time type checks. The deepcopy procedure from
the Deep Copy task is sufficient. The sample code shown below is
Unicon-specific only because it is copying an object (Icon has no
object-oriented support). The deepcopy procedure is identical in both
lanaguages.
 
<lang unicon>class T()
method a(); write("This is T's a"); end
end
 
class S: T()
method a(); write("This is S's a"); end
end
 
procedure main()
write("S:",deepcopy(S()).a())
end
 
procedure deepcopy(A, cache) #: return a deepcopy of A
local k
/cache := table() # used to handle multireferenced objects
if \cache[A] then return cache[A]
case type(A) of {
"table"|"list": {
cache[A] := copy(A)
every cache[A][k := key(A)] := deepcopy(A[k], cache)
}
"set": {
cache[A] := set()
every insert(cache[A], deepcopy(!A, cache))
}
default: { # records and objects (encoded as records)
cache[A] := copy(A)
if match("record ",image(A)) then {
every cache[A][k := key(A)] := deepcopy(A[k], cache)
}
}
}
return .cache[A]
end</lang>
 
Sample run:
<pre>
->polycopy
This is S's a
->
</pre>