![]() |
![]() |
Basic Ideas |
![]() |
![]() |
![]() |
||||
![]() |
![]() |
![]() |
![]() |
![]() |
My first experiences with OO extensions in Tcl/Tk was Itcl which I used intensively on programming my CMS One Hand Content. This extension worked mostly as expected – with a few pitfalls and one thing I found annoying: options were mapped to internal variables of the objects. More than once I ran in danger of name collision inside method definitions. Then, I found Snit and really enjoyed it. Especially, options are mapped to key/value pairs of an array. No more danger of name collision! — Instead of heritage, components are installed where methods and options can be delegated to: really great! — But, I found the hint that performance disappoints if there are many once-used local objects. But that was exactly what I wanted in my current project on computer-generated cartoons. So, I made obj — just another OO system. It does not seamlessly fit into Tk's object approach, and it is far from being complete. But it is minimalistic, and it is (at least intended to be) quite fast. It is made as follows: LocationThe basic procedures are located in namespace A method is just a procedure where the instance name is first argument. The first argument variable is fixed to the name Instance creationBesides a few pre-defined prcedures and methods, here resides the basic procedure An instance is created as follows:
The consequences are:
No heritageObj does not provide class heritage as used by Itcl, but instead component delegation as used by Snit, as follows: Besides options, an instance $o private key val If $o component key method1 ... Internal valuesDifferently to other object systems, no mapping to Tcl variables, instead instance-internal values are accessible only by the methods Differently to Itcl, methods are realized as procedures, but not mapped to procedures. Inside method definitions, own methods must always invoked via DelegationIf you wish that every instance of class % ::obj::constructor dog {} { $self private tail [new tail] } constructor dog % If you wish that every method call % ::obj::delegate method wag dog tail class dog delegates method wag to component tail. % You can do the same with options, e.g. InteroperabilityIf you work with inherited objects of other object systems, e. g. Snit, or Itcl, then you can use their instances as components of obj instances. They live peacefully side-by-side. As delegation of methods and options just triggers an appropriate method definition, delegating obj things to Itcl components works as well as to own objects! |