Updated 2017-06-04 23:53:52 by pooryorick

my, a built-in Tcl command, allows one method of an object to invoke another method of the same object.

Documentation  edit

official reference

Description  edit

my invokes any method, including non-exported methods, defined by current object, i.e., the object that the method which calls my is bound to.

my is actually a separate command that is created in each object's namespace, so it can be invoked from outside the object if you know the name of the namespace or if you use namespace code inside the object to wrap the invocation. This is useful for callbacks like variable traces or bind event handlers, and also for oo::objdefine foward and oo::define forward.

Invoking without my  edit

PYK 2017-06-04:
proc oo::objdefine::baremethod {name args body} {
    set object [lindex [info level -1] 1]
    ::oo::objdefine $object [list method $name $args $body]
    proc [info object namespace $object]::$name args [
        list ::tailcall my $name {*}$args]
}

A simple example:
::oo::object create o1
::oo::objdefine o1 {
    baremethod m1 {} {
        puts {hello from m1}
    }
    baremethod m2 {} {
        m1
        puts {hello from m2}
    }
}

o1 m2

This works for object systems that use oo::copy and avoid oo::class. Doing the same for objects created via the oo::class mechanism is less feasible.