Version 3 of xotcl for prototyping

Updated 2006-09-18 11:53:24

Object Oriented facilities gives higher abstraction power. But iterative devleopment is mostly bottom-up. One tries out some function, then finds some repetition and make it a generic facility. The typical Xotcl tutorial starts with class. Mostly in protoyping one does not start with class.Here we will see how xotcl is employed in prototyping.

Say we have a need for counter. Now, there are action, like add,decr on a counter. The functions add,decr share a common state variable, count.

First approach. count as global variable, add and decr acting on that.

Second iteration. We decide not to pollute the global space, so we need an isolated context. We have two choices , either namespace or using object. We will use the second

using XOTcl

 Object counter

 counter set count 0

 counter proc add { val } {
  my set count [expr [my set count]  + $val] 
 } 

 counter proc decr { val } {
  my set count [expr [my set count] - $val]
 }

Now, we encounter a need for another counter object in the program. We can define a class. But the system should not force you. One should accomplish instantiation by other means too. So that we can abstract/generalise whenever we want to. Instantiation by cloning.

 counter copy counter2
 counter2 set count 0

Here using copy, we can clone an object and thereby we can create multiple instance of an object. Here comes a major limitation, if we adopted namespace approach. namespace and object define an isolated context, but namespace does not allow cloning to give multiple instances.

Now we decide, we will have some kind of factory object(class object), that can create multiple instances of counter object on demand.

( Somehow fill in how to create a class object from an already existing instance. is it possible in Xotcl).

vkvalli