Index: Makefile.in =================================================================== diff -u -rf769aa3bf33311a58e91f5041130d34daf840e7c -rcc85907efa33d1b8505791285175769571f6f9e4 --- Makefile.in (.../Makefile.in) (revision f769aa3bf33311a58e91f5041130d34daf840e7c) +++ Makefile.in (.../Makefile.in) (revision cc85907efa33d1b8505791285175769571f6f9e4) @@ -258,9 +258,9 @@ $(src_doc_dir)/example-scripts/traits-composite.html \ $(src_doc_dir)/example-scripts/traits-simple.html \ $(src_doc_dir)/example-scripts/rosetta-tokenizer.html \ - $(src_doc_dir)/example-scripts/rosetta-tree.html + $(src_doc_dir)/example-scripts/rosetta-tree.html \ + $(src_doc_dir)/example-scripts/rosetta-multiple-distinct.html - %.html : %.tcl $(TCLSH) $(src_app_dir_native)/utils/source-doc-beautifier.tcl $< PATH=$(src_app_dir_native)/utils:$(PATH) asciidoc -f $(src_app_dir_native)/utils/asciidoc.conf $*.txt @@ -591,6 +591,7 @@ $(TCLSH) $(src_doc_dir_native)/example-scripts/traits-simple.tcl -libdir $(PLATFORM_DIR) $(TESTFLAGS) $(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) test-xotcl: $(TCLSH_PROG) $(TCLSH) $(xotcl_src_test_dir)/testo.xotcl -libdir $(PLATFORM_DIR) $(TESTFLAGS) Index: TODO =================================================================== diff -u -r4421b3ab2a573245c2ae2a84e31c2be244ec4a74 -rcc85907efa33d1b8505791285175769571f6f9e4 --- TODO (.../TODO) (revision 4421b3ab2a573245c2ae2a84e31c2be244ec4a74) +++ TODO (.../TODO) (revision cc85907efa33d1b8505791285175769571f6f9e4) @@ -5839,6 +5839,9 @@ - Added Rosetta example: https://rosettacode.org/wiki/Tree_traversal +- Added Rosetta example: https://rosettacode.org/wiki/Multiple_distinct_objects + + ======================================================================== TODO: @@ -5872,7 +5875,6 @@ - Add Rosetta examples: (for the sake of completeness) - https://rosettacode.org/wiki/Multiple_distinct_objects https://rosettacode.org/wiki/Add_a_variable_to_a_class_instance_at_runtime https://rosettacode.org/wiki/Inheritance/Single https://rosettacode.org/wiki/Inheritance/Multiple#Tcl Index: doc/example-scripts/rosetta-multiple-distinct.html =================================================================== diff -u --- doc/example-scripts/rosetta-multiple-distinct.html (revision 0) +++ doc/example-scripts/rosetta-multiple-distinct.html (revision cc85907efa33d1b8505791285175769571f6f9e4) @@ -0,0 +1,830 @@ + + + + + +Listing of doc/example-scripts/rosetta-multiple-distinct.tcl + + + + + +
+
+

Rosetta example: Multiple distinct objects

+
+

Create a sequence (array, list, whatever) consisting of n distinct, +initialized items of the same type. n should be determined at +runtime.

+ +
+
+
package req nx
+

The class Klass defines and implements the item type. It can also +be used to query its population of instances.

+
+
+
nx::Class create Klass
+set n 100; # runtime parameter
+

Wrong: Only a single item (object) is created, its (command) name is replicated n times.

+
+
+
% llength [Klass info instances]
+0;
+
+set theList [lrepeat $n [Klass new]]
+
+% llength [Klass info instances]
+1;
+% llength [lsort -unique $theList]
+1;
+
+[lindex $theList 0] destroy
+

Correct: n items (objects) having distinct (command) names are +created and stored in the list.

+
+
+
% llength [Klass info instances]
+0;
+
+set theList {}
+
+for {set i 0} {$i<$n} {incr i} {
+    lappend theList [Klass new]
+}
+
+% llength [Klass info instances]
+$n;
+% llength [lsort -unique $theList]
+$n;
+
+
+
+

+ + + Index: doc/example-scripts/rosetta-multiple-distinct.tcl =================================================================== diff -u --- doc/example-scripts/rosetta-multiple-distinct.tcl (revision 0) +++ doc/example-scripts/rosetta-multiple-distinct.tcl (revision cc85907efa33d1b8505791285175769571f6f9e4) @@ -0,0 +1,50 @@ +# +# == Rosetta example: Multiple distinct objects +# +# +# Create a sequence (array, list, whatever) consisting of n distinct, +# initialized items of the same type. n should be determined at +# runtime. +# +# https://rosettacode.org/wiki/Multiple_distinct_objects +# + +package req nx +package req nx::test + +# +# The class +Klass+ defines and implements the item type. It can also +# be used to query its population of instances. +# + +nx::Class create Klass +set n 100; # runtime parameter + +# +# Wrong: Only a single item (object) is created, its (command) name is replicated +n+ times. +# + +? {llength [Klass info instances]} 0; + +set theList [lrepeat $n [Klass new]] + +? {llength [Klass info instances]} 1; +? {llength [lsort -unique $theList]} 1; + +[lindex $theList 0] destroy + +# +# Correct: +n+ items (objects) having distinct (command) names are +# created and stored in the list. +# + +? {llength [Klass info instances]} 0; + +set theList {} + +for {set i 0} {$i<$n} {incr i} { + lappend theList [Klass new] +} + +? {llength [Klass info instances]} $n; +? {llength [lsort -unique $theList]} $n;