Index: Makefile.in =================================================================== diff -u -red0f5cba982af963b62c36d8c8c75e89c81adf3e -r922c0ab9c756265f0793bda2cc69d50742c5bf88 --- Makefile.in (.../Makefile.in) (revision ed0f5cba982af963b62c36d8c8c75e89c81adf3e) +++ Makefile.in (.../Makefile.in) (revision 922c0ab9c756265f0793bda2cc69d50742c5bf88) @@ -260,7 +260,8 @@ $(src_doc_dir)/example-scripts/rosetta-tokenizer.html \ $(src_doc_dir)/example-scripts/rosetta-tree.html \ $(src_doc_dir)/example-scripts/rosetta-multiple-distinct.html \ - $(src_doc_dir)/example-scripts/rosetta-add-variable.html + $(src_doc_dir)/example-scripts/rosetta-add-variable.html \ + $(src_doc_dir)/example-scripts/rosetta-clone.html %.html : %.tcl $(TCLSH) $(src_app_dir_native)/utils/source-doc-beautifier.tcl $< @@ -593,7 +594,8 @@ $(TCLSH) $(src_doc_dir_native)/example-scripts/rosetta-tokenizer.tcl -libdir $(PLATFORM_DIR) $(TESTFLAGS) $(TCLSH) $(src_doc_dir_native)/example-scripts/rosetta-tree.tcl -libdir $(PLATFORM_DIR) $(TESTFLAGS) $(TCLSH) $(src_doc_dir_native)/example-scripts/rosetta-multiple-distinct.tcl -libdir $(PLATFORM_DIR) $(TESTFLAGS) - $(TCLSH) $(src_doc_dir_native)/example-scripts/rosetta-add-variable.tcl -libdir $(PLATFORM_DIR) $(TESTFLAGS) + $(TCLSH) $(src_doc_dir_native)/example-scripts/rosetta-add-variable.tcl -libdir $(PLATFORM_DIR) $(TESTFLAGS) + $(TCLSH) $(src_doc_dir_native)/example-scripts/rosetta-clone.tcl -libdir $(PLATFORM_DIR) $(TESTFLAGS) test-xotcl: $(TCLSH_PROG) $(TCLSH) $(xotcl_src_test_dir)/testo.xotcl -libdir $(PLATFORM_DIR) $(TESTFLAGS) Index: TODO =================================================================== diff -u -r4b78042d08af7f3200d51f4033d5164c949f332d -r922c0ab9c756265f0793bda2cc69d50742c5bf88 --- TODO (.../TODO) (revision 4b78042d08af7f3200d51f4033d5164c949f332d) +++ TODO (.../TODO) (revision 922c0ab9c756265f0793bda2cc69d50742c5bf88) @@ -5855,6 +5855,8 @@ proc =foo {a b c args} { puts foo } catch {={*}[list foo 1 2 3 4 5 6]} msg; puts msg=$msg +- Added Rosetta example: https://rosettacode.org/wiki/Polymorphic_copy#Tcl + ======================================================================== TODO: @@ -5876,7 +5878,6 @@ - Add Rosetta examples: (for the sake of completeness) - https://rosettacode.org/wiki/Polymorphic_copy#Tcl https://rosettacode.org/wiki/Inheritance/Single https://rosettacode.org/wiki/Inheritance/Multiple#Tcl Index: doc/example-scripts/rosetta-clone.html =================================================================== diff -u --- doc/example-scripts/rosetta-clone.html (revision 0) +++ doc/example-scripts/rosetta-clone.html (revision 922c0ab9c756265f0793bda2cc69d50742c5bf88) @@ -0,0 +1,874 @@ + + + + + +Listing of doc/example-scripts/rosetta-clone.tcl + + + + + +
+
+

Rosetta example: Polymorphic copy

+
+

Let a polymorphic object contain an instance of some specific type S +derived from a type T. The type T is known. The type S is possibly +unknown until run time. The objective is to create an exact copy of +such polymorphic object.

+ +
+
+
package req nx
+

nx::Object provides a method copy which creates a deep copy of any +source object (hence, polymorphic in the sense of this task), i.e., +it contains all structural and behavioral features of the source and +preserves its signature type.

+
+
+
nx::Class create T {
+    :property -accessor public {label "T"}
+}
+nx::Class create S -superclasses T {
+    :property -accessor public {label "S"}
+}
+
+set client [nx::Object new {
+    :public object method duplicate {src} {
+        # this is the polymorphic call site
+        return [$src copy]
+    }
+}]
+
+set t [T new]
+% $t label get
+T
+set s [S new]
+% $s label get
+S
+

Provide two copies, using copy underneath

+
+
+
set t2 [$client duplicate $t]
+set s2 [$client duplicate $s]
+

Are the copies truly independent objects (identities)? Yes …

+
+
+
% expr {$t2 ne $t}
+1
+% expr {$s2 ne $s}
+1
+

Are the copies offsprings of the source types/classes? Yes …

+
+
+
% $t info class
+::T
+% $t2 info class
+::T
+
+% $s info class
+::S
+% $s2 info class
+::S
+

Do the copies operate exactly like their source objects? Yes …

+
+
+
% $t label get
+T
+% $t2 label get
+T
+
+% $s label get
+S
+% $s2 label get
+S
+
+
+
+

+ + + Index: doc/example-scripts/rosetta-clone.tcl =================================================================== diff -u --- doc/example-scripts/rosetta-clone.tcl (revision 0) +++ doc/example-scripts/rosetta-clone.tcl (revision 922c0ab9c756265f0793bda2cc69d50742c5bf88) @@ -0,0 +1,73 @@ +# +# == Rosetta example: Polymorphic copy +# +# Let a polymorphic object contain an instance of some specific type S +# derived from a type T. The type T is known. The type S is possibly +# unknown until run time. The objective is to create an exact copy of +# such polymorphic object. + +# https://rosettacode.org/wiki/Polymorphic_copy#Ruby +# + +package req nx +package req nx::test + +# +# nx::Object provides a method +copy+ which creates a deep copy of any +# source object (hence, polymorphic in the sense of this task), i.e., +# it contains all structural and behavioral features of the source and +# preserves its signature type. +# + +nx::Class create T { + :property -accessor public {label "T"} +} +nx::Class create S -superclasses T { + :property -accessor public {label "S"} +} + +set client [nx::Object new { + :public object method duplicate {src} { + # this is the polymorphic call site + return [$src copy] + } +}] + +set t [T new] +? {$t label get} "T" +set s [S new] +? {$s label get} "S" + +# +# Provide two copies, using +copy+ underneath +# +set t2 [$client duplicate $t] +set s2 [$client duplicate $s] + +# +# Are the copies truly independent objects (identities)? Yes ... +# + +? {expr {$t2 ne $t}} 1 +? {expr {$s2 ne $s}} 1 + +# +# Are the copies offsprings of the source types/classes? Yes ... +# + +? {$t info class} "::T" +? {$t2 info class} "::T" + +? {$s info class} "::S" +? {$s2 info class} "::S" + +# +# Do the copies operate exactly like their source objects? Yes ... +# + +? {$t label get} "T" +? {$t2 label get} "T" + +? {$s label get} "S" +? {$s2 label get} "S" +