Objects in Tcl - iTcl - updated first steps example
Archive - Originally posted on "The Horse's Mouth" - 2015-03-11 11:28:12 - Graham EllisiTcl is Tcl's Object Oriented package, and it's shipped as standard with Tcl these days.
On many of our OO courses, we start off with an example of "train" objects and define and run methods such as "getCapacity" on them, and today I've taken that same example and coded it in Tcl - see [here].
My main (test) code creates two trains and puts them into a list:
set ocount 0
set s1 [train train_[incr ocount] 2 75 "06:38" "Southampton Central"]
set s2 [train train_[incr ocount] 1 78 "07:49" "Swindon"]
set services [list $s1 $s2]
and then loops through and prints out the capacity of each:
foreach service $services {
set capacity [$service getcapacity]
puts "That can take $capacity people"
}
iTcl provides default cget and configure methods on each object through which you can directly access variables which are declared public within objects - so I can extend the example above:
$s1 configure -where "Marchwood"
foreach service $services {
set capacity [$service getcapacity]
set to [$service cget -where]
puts "That can take $capacity people to $to"
}
Source code [here].