Index: doc/example-scripts/rosetta-constraint-genericity.html =================================================================== diff -u -r93bb0947d582f274afb1cdbc885909d55e100b36 -r24cb8f4bffd49c9375c1c64aa0610933b62511bb --- doc/example-scripts/rosetta-constraint-genericity.html (.../rosetta-constraint-genericity.html) (revision 93bb0947d582f274afb1cdbc885909d55e100b36) +++ doc/example-scripts/rosetta-constraint-genericity.html (.../rosetta-constraint-genericity.html) (revision 24cb8f4bffd49c9375c1c64aa0610933b62511bb) @@ -3,7 +3,7 @@ - + Listing of doc/example-scripts/rosetta-constraint-genericity.tcl +
package req nx

Define the two classes Eatable and Fish. Eatable is a class for all eatable things, a Fish is a subclass ant therefore eatable.

-
+
+
nx::Class create Eatable
+nx::Class create Fish -superclass Eatable {
+  :property name
+}

A FoodBax may only contain eatable items. Therefore with we define items as a property of type Eatable" which has a multiplicity of +0..n (might contain 0 to n eatable items). Furthermore, we define items as incremental, such we can add / remove items with item add or item remove.

-
+
+
nx::Class create FoodBox {
+  :property -incremental item:object,type=::Eatable
+  :public method print {} {
+    set string "Foodbox contains:\n"
+    foreach i ${:item} {append string "   [$i cget -name]\n"}
+    return $string
+  }
+}

Demonstrating the behavior in a shell:

Create two fishes, Wanda and Nemo:

-
+
+
% set f1 [Fish new -name "Wanda"]
+% set f2 [Fish new -name "Nemo"]

Create a Foodbox and add the two fishes:

-
+
+
% set fb [FoodBox new]
+% $fb item add $f1
+% $fb item add $f2

Return the print string of the contents:

-
+
+
% $fb print
+Foodbox contains:
+   Nemo
+   Wanda