Index: Makefile.in =================================================================== diff -u -rcc85907efa33d1b8505791285175769571f6f9e4 -red0f5cba982af963b62c36d8c8c75e89c81adf3e --- Makefile.in (.../Makefile.in) (revision cc85907efa33d1b8505791285175769571f6f9e4) +++ Makefile.in (.../Makefile.in) (revision ed0f5cba982af963b62c36d8c8c75e89c81adf3e) @@ -259,7 +259,8 @@ $(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-multiple-distinct.html + $(src_doc_dir)/example-scripts/rosetta-multiple-distinct.html \ + $(src_doc_dir)/example-scripts/rosetta-add-variable.html %.html : %.tcl $(TCLSH) $(src_app_dir_native)/utils/source-doc-beautifier.tcl $< @@ -591,7 +592,8 @@ $(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) + $(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) test-xotcl: $(TCLSH_PROG) $(TCLSH) $(xotcl_src_test_dir)/testo.xotcl -libdir $(PLATFORM_DIR) $(TESTFLAGS) Index: TODO =================================================================== diff -u -rcc85907efa33d1b8505791285175769571f6f9e4 -red0f5cba982af963b62c36d8c8c75e89c81adf3e --- TODO (.../TODO) (revision cc85907efa33d1b8505791285175769571f6f9e4) +++ TODO (.../TODO) (revision ed0f5cba982af963b62c36d8c8c75e89c81adf3e) @@ -5841,6 +5841,7 @@ - Added Rosetta example: https://rosettacode.org/wiki/Multiple_distinct_objects +- Added Rosetta example: https://rosettacode.org/wiki/Add_a_variable_to_a_class_instance_at_runtime ======================================================================== TODO: @@ -5875,10 +5876,9 @@ - Add Rosetta examples: (for the sake of completeness) - https://rosettacode.org/wiki/Add_a_variable_to_a_class_instance_at_runtime + https://rosettacode.org/wiki/Polymorphic_copy#Tcl https://rosettacode.org/wiki/Inheritance/Single https://rosettacode.org/wiki/Inheritance/Multiple#Tcl - https://rosettacode.org/wiki/Polymorphic_copy#Tcl (more substantial) https://rosettacode.org/wiki/Active_object Index: doc/example-scripts/rosetta-add-variable.html =================================================================== diff -u --- doc/example-scripts/rosetta-add-variable.html (revision 0) +++ doc/example-scripts/rosetta-add-variable.html (revision ed0f5cba982af963b62c36d8c8c75e89c81adf3e) @@ -0,0 +1,970 @@ + + + + + +Listing of doc/example-scripts/rosetta-add-variable.tcl + + + + + +
+
+

Rosetta example: Add a variable to a class instance at runtime

+
+

Demonstrate how to dynamically add variables to an object (a class instance) at runtime.

+ +
+
+
package req nx
+

The class Empty does not provide any structural or behaviora +features on behalf of future instances, they will remain empty.

+
+
+
nx::Class create Empty
+

Provide one instance of Empty to add an object variable to …

+
+
+
Empty create ::e
+

Is e truly empty?

+
+
+
% ::e info vars
+

NX offers different types of object-variable managers: properties, +variable slots, and plain Tcl per-object variables. Below, we +showcase variable slots (although the others can be added +dynamically alike).

+

a) Declare a variable slot foo; -accessor will provide +getter/setter methods for this one object e automatically.

+
+
+
::e object variable -accessor public foo
+

b) Define a value for the variable slot foo

+
+
+
% ::e foo set 1
+1
+

c) Is there a Tcl variable managed by the variable slot?

+
+
+
% ::e info vars
+foo
+

d) Retrieve foo's value

+
+
+
% ::e foo get
+1
+

A second instance of Empty has no such capability: foo

+
+
+
Empty create ::f
+

a) Is there any Tcl variable, one named foo? No …

+
+
+
% ::f info vars
+

b) Are there getter/setter methods for a foo? No …

+
+
+
% ::f foo set
+::f: unable to dispatch method 'foo'
+

c) Is there a variable slot foo? No …

+
+
+
% ::f info object variables foo
+

In NX, once dynamically added, a variable slot can also be dynamically removed again.

+
+
+
::e delete object variable foo
+

a) Is the variable slot foo gone? Yes …

+
+
+
% ::e info object variables foo
+

b) Is the Tcl variable gone? Yes …

+
+
+
% ::e info vars
+

c) Are the getter/setter methods gone? Yes …

+
+
+
% ::e foo get
+::e: unable to dispatch method 'foo'
+
+
+
+

+ + + Index: doc/example-scripts/rosetta-add-variable.tcl =================================================================== diff -u --- doc/example-scripts/rosetta-add-variable.tcl (revision 0) +++ doc/example-scripts/rosetta-add-variable.tcl (revision ed0f5cba982af963b62c36d8c8c75e89c81adf3e) @@ -0,0 +1,76 @@ +# +# == Rosetta example: Add a variable to a class instance at runtime +# +# Demonstrate how to dynamically add variables to an object (a class instance) at runtime. + +# https://rosettacode.org/wiki/Add_a_variable_to_a_class_instance_at_runtime#Tcl +# + +package req nx +package req nx::test + +# +# The class +Empty+ does not provide any structural or behaviora +# features on behalf of future instances, they will remain empty. +# + +nx::Class create Empty + +# +# Provide one instance of +Empty+ to add an object variable to ... +# + +Empty create ::e + +# Is +e+ truly empty? + +? {::e info vars} "" + +# +# NX offers different types of object-variable managers: properties, +# variable slots, and plain Tcl per-object variables. Below, we +# showcase variable slots (although the others can be added +# dynamically alike). +# + +# a) Declare a variable slot +foo+; +-accessor+ will provide +# getter/setter methods for this one object +e+ automatically. + +::e object variable -accessor public foo + +# b) Define a value for the variable slot +foo+ +? {::e foo set 1} 1 + +# c) Is there a Tcl variable managed by the variable slot? +? {::e info vars} "foo" + +# d) Retrieve +foo+'s value +? {::e foo get} 1 + +# +# A second instance of +Empty+ has no such capability: +foo+ +# + +Empty create ::f + +# a) Is there any Tcl variable, one named +foo+? No ... +? {::f info vars} "" +# b) Are there getter/setter methods for a +foo+? No ... +? {::f foo set} "::f: unable to dispatch method 'foo'" +# c) Is there a variable slot +foo+? No ... +? {::f info object variables foo} "" + +# +# In NX, once dynamically added, a variable slot can also be dynamically removed again. +# + +::e delete object variable foo + +# a) Is the variable slot +foo+ gone? Yes ... +? {::e info object variables foo} "" + +# b) Is the Tcl variable gone? Yes ... +? {::e info vars} {} + +# c) Are the getter/setter methods gone? Yes ... +? {::e foo get} "::e: unable to dispatch method 'foo'"