scriptable OO

TclOO is not just an OO extension, it's a scriptable OO extension.

It's possible to add facilities and capabilities to the oo::class object, and enhance or modify the kind of objects class creates.

Examples (developed in 8 minutes flat by dkf)

Class Methods

        proc ::oo::define::classmethod name {
            set class [lindex [info level -1] 1]
            set classmy [info object namespace $class]::my
            uplevel 1 forward $name $classmy $name
        }

Class Variables

        proc ::oo::Helpers::classvar {name args} {
            set ns [info object namespace [uplevel 1 {self class}]]
            foreach v [list $name {*}$args] {
                uplevel 1 [list namespace upvar $ns $v $v]
            }
        }

Usage

        oo::class create Foo {
          method bar1 x {
           classvar y
           incr y $x
           my bar2
          }
          self variable y   
          self method bar2 {} {puts $y,$y,$y}
          classmethod bar2
        }


dkf - 2009-07-27 06:05:20

I'm not quite sure that I got the interface for class methods right. Here's what a few more minutes-worth of thought leads to:

proc ::oo::define::classmethod {name {args {}} {body {}}} {
    set class [lindex [info level -1] 1]
    set classmy [info object namespace $class]::my
    if {[llength [info level 0]] == 4} {
        uplevel 1 [list self method $name $args $body]
    }
    uplevel 1 forward $name $classmy $name
}

Like that, if you give extra arguments you don't just couple to the class definition, you also create that definition.