Version 3 of A simple comparison of Tcl object extensions

Updated 2004-01-22 08:21:47

In January of 2004, a poster to comp.lang.tcl asked which of the tcl extensions providing object orientation allowed one to define class members which were other objects.

Several people submitted answers - and some of the answers provided demonstrated how the different extensions might do just that.

Here's some of the examples provided:


Snit

 snit::type C2 {
    variable this
    variable that

    constructor {args} {
        set this [C2 %AUTO%]
        set that [C2 %AUTO%]
    }

    destructor {
        catch {$this destroy}
        catch {$that destroy}
    }

    method doThis {} {
        $this doSomething
    }

    method doThat {} {
        $that doSomething
    }
 }

incr Tcl

  itcl::class C1 {
  }

  itcl::class C2 {
      variable m1
      variable m2
      constructor {} {
          set m1 [C1 #auto]
          set m2 [C1 #auto]
      }
      destructor {
          itcl::delete object $m1 $m2
      }
  }

stooop

 class C2 {
     proc C2 {this} {
         set ($this,o1) [new C1]
         set ($this,o2) [new C1]
     }
     proc ~C2 {this} {
         delete $($this,o1) $($this,o2)
     }
     class C1 {
         proc C1 {this} {}
         proc ~C1 {this} {}
     }
 }
 delete [new C2]

XOTcl

There are some posibilities

Example 1 (by using instance variables)

   Class C1
   Class C2
   C2 instproc init {} {
       my instvar o1 o2
       set o1 [C1 new]
       set o2 [C1 new]
   }
   C2 instproc destroy {} {
       my instvar o1 o2
       $o1 destroy
       $o2 destroy 
       next
   }

Example 2 (by using nested objects and parameters. Does not need to destroy explicit the objects)

Subobject are butter to implement aggragation

   Class C1
   Class C2 -parameter {o1 o2}
   C2 instproc init {} {
       my o1 [C1 new -childof [self]]
       my o2 [C1 new -childod [self]]
   }

Example 3 (by using named subobjects. Does not need to destroy explicit the objects)

Quite similar to C++ object members

   Class C1
   Class C2
   C2 instproc init {} {
       C1 create [self]::o1
       C1 create [self]::o2  
   }
   C2 intporc useO1 {} {
       puts "class of o1 [[self]::o1 info class]"
   }

Category Object Orientation | Category Example