OTcl ( MIT Object TCL )

[ Back to Network Simulator 2 for Wireless Home Page ]

1. The constructor function of a class (object) is:


The init instproc is used to initialize a freshly allocated object that is a direct or indirect instance of the class Object. It is normally called by the system (perhaps from more specialized init instprocs) as part of object creation, but may be called by the user.

init interprets its arguments as pairs of option keys and option values. Each option key should be the name of a valid method for the object, preceded by a dash. The method should take one argument. For each option key and option value pair, init calls the method on the object, with the option value as its argument. It returns the empty string.

To customize object creation, write an init instproc for a class, not an alloc proc. If the option key and option value creation syntax is still desired, then call the Object init instproc by using next. This is discussed in the create instproc in OTcl Classes.

% Class Bagel
% foreach i {1 2 3 4} {
Bagel instproc $i {v} {puts $v}
% Bagel abagel
% abagel init -1 one -2 two -3 three -4 four!

init is conceptually equivalent to the following.

Object instproc init {args} {                   
if {[llength $args]%2 != 0} then {
error {uneven number of arguments}
while {$args != {}} {
set key [lindex $args 0]
if {[string match {-*} $key]} then {
set key [string range $key 1 end]
set val [lindex $args 1]
if {[catch {$self $key $val} msg]!=0} then {
set opt [list $self $key $val]
error "$msg during $opt"
set args [lrange $args 2 end]
return {}