Polymorphic copy: Difference between revisions

(Nimrod -> Nim)
Line 679:
tcopy => print \ use "print" methods
scopy => print</lang>
 
 
Works with any ANS Forth
 
Needs the FMS-SI (single inheritance) library code located here:
http://soton.mpeforth.com/flag/fms/index.html
<lang forth>include FMS-SI.f
 
:class T <super object
ivar container \ can eventually contain an object of any type
:m put ( obj -- ) container ! ;m
:m init: [self] [self] put ;m \ initially container holds self
:m print ." class is T" ;m
:m print-container container @ print ;m
;class
 
:class S <super T \ subclass S from T
:m print ." class is S" ;m \ override T's print method
;class
 
: ecopy {: obj1 -- obj2 :} \ make an exact copy of obj
obj1 dup >class dfa @
obj1 heap: dup >r swap move r> ;
T obj-A \ instantiate a T object
obj-A print-container \ class is T
 
S obj-B \ instantiate an S object
obj-B ecopy obj-A put \ make an exact copy of S object and store in T object
 
obj-A print-container \ class is S
</lang>
 
=={{header|Go}}==