gilTclObjectClass creation gilTclObject is a Tcl extension that adds object programming constructs to the Tcl script language. The services that are implemented are:
There are many other object extensions to Tcl. Plus, Tcl 8.6 will have built-in object orientation. gilTclObject was created because no other extensions seemed to have the feature set we needed. See the design philosophy to know more. ActivationgilTclObject is available either as a pure tcl extension (i.e. a single textual script) or as a binary extension. The pure tcl implementation is very easily activated, simply source the file: source ${where_gil_is_installed}/src/srcipt/disabled/gil_Object.tcl and you are done: the method command and its companions have been added to your tcl interpreter. The binary extension offers the benefit of performance, and a more transparent implementation (error messages are cleaner than in the pure Tcl implementation), but it requires to either download a compatible shared library, or compile it from sources. Once the shared library is available, the activation is done by: load ${where_gil_is_installed}/bin/osx/libgiltclobject.dylib | load ${where_gil_is_installed}/bin/x11/libgiltclobject.so | load ${where_gil_is_installed}/bin/win/giltclobject.dll depending on the platform. Class creationA class is created at the first method definition for the class: method NewClass NewMethod ... Object creationAn object (an instance) is created by calling the Tcl command which name is the class name, and by passing the name of the object as the first parameter. NewClass newObject When an object is created, a Tcl associative array with the same name is created at the global level for storing the object's attributes (see Method creation). A Tcl command with the same name is also created at the global level for method invocation on the object (see Method call below). Method creationA method is created by calling the method command and passing it 4 parameters:
The method command is modeled after the proc Tcl command, only the class name must be added in front on the method name. In the body of the method, the associative array this is used to access the object's attributes (see example below). The associative array class is used to access the class attributes (static attributes in C++). Three variable are implicitly defined: objName (the object's name), className (the method 's class, which can be different from the object's class), and methodName (the name of the method). These three variables should not be modified. method NewClass NewMethod { a b } { puts "Calling method $methodName of class $className on object $objName" set this(a) $a set this(b) $b incr class(objCount) } Method callA method is called on an object by using the Tcl command which name is the name of the object, and by passing the method name as the first parameter, followed by the method's parameters: newObject NewMethod 1 2 Within a method body, this is used as an associative array (see Method creation), but also as a Tcl command for calling methods on the current object: method ... { this anotherMethod ... } InheritanceA class inherits the methods of a super class with the inherit Tcl command. inherit SpecializedClass SuperClass If the class SpecializedClass does not exist when calling inherit, then it is created. If SuperClass does not exist, then an attempt to source its definition is made with the Tcl auto_load mechanism. By default, all classes inherit from the class gilObjRootClass which defines the dispose method (among others) for object disposal. The dispose method must be called to destroy an object. newObject dispose A call to the method of a specific class is done by prefixing the method name by the class name and "::": this SuperClass::NewMethod In the body of a method that specialize and inherited method, the inherited method is called by using inherited as the method name: this inherited x y z where x, y, and z are three parameters passed to the inherited method. Constructors and DestructorsIf a class has a method named "constructor", the this method is called after an object of this class is created. A constructor can't return a value (the returned value is ignored). method IntClass constructor { initialValue } { set this(i) $initialValue } IntClass anInt 12 If a class has a method named "destructor", then this method is called before the destruction of an object. A destructor cannot receive any parameter nor return a value. Other sevicesThe unknown methodIf a call is made to an undefined method on an object, then the call is replaced by a call the the unknown method and inserting the undefined method name as the first parameter. For example, is the object newObject has no method named someMethod, then the call: newObject someMethod a b c is replaced by newObject unknown someMethod a b c By default, all object inherit the unknown method from the gilObjRootClass. The method simply abort the script evaluation by generating an error message. Specializations can defined their own behavior by providing their own version of the unkown method. Information of objects / classes (introspection)The gilObject info command is used to get informations about classes, methods, and objects. For example, the full definition of a class is returned by: gilObject info class <NomDeLaClasse> Auto-loadingThe Tcl auto loading mechanism (see auto_load) can be used to load class definition files automatically. The "src/script/gil_MakeIndex.tcl" script must be evaluated in every directories that contain class definition script files. Design PhilosophyThe design goal of gilTclObject was to create an extension that is as lightweight as possible in term of learning and usage, while still providing all the necessary object orientation constructs. Thus, the extension is modeled closely after Tcl/Tk. Learning is reduced because gilTclObject adds very little to the knowledge required to use Tcl/Tk: method creation is almost the same as Tcl procedure creation, with the simple addition of a class name is front of the method name. Object creation and method invocation mimic Tk's widget creation and widget command invocation. In addition, gilTclObject uses the same paradigm as Tcl for lightweight programming: classes, attributes and methods are only created when needed. |