Skip to content
Tcl Source Code Artifact [aa86295a53]
Login

Artifact [aa86295a53]

Artifact aa86295a53bbdfd00124d07e5ce57053f6e58a545164743dc8b54489b53b1282:



CommandName: my ManualSection: n Version: 0.1 TclPart: TclOO TclDescription: TclOO Commands Links:

Name

my, myclass - invoke any method of current object or its class

Synopsis

::: {.synopsis} ::: [package]{.cmd} [require]{.sub} [tcl::oo]{.lit}

[my]{.cmd} [methodName]{.arg} [arg]{.optdot} [myclass]{.cmd} [methodName]{.arg} [arg]{.optdot} :::

Description

The fBmyfR command is used to allow methods of objects to invoke methods of the object (or its class),

::: {.info -version="TIP478"} and the fBmyclassfR command is used to allow methods of objects to invoke methods of the current class of the object fIas an objectfR. :::

In particular, the set of valid values for fImethodNamefR is the set of all methods supported by an object and its superclasses, including those that are not exported

::: {.info -version="TIP500"} and private methods of the object or class when used within another method defined by that object or class. :::

The object upon which the method is invoked via fBmyfR is the one that owns the namespace that the fBmyfR command is contained in initially (fBNB:fR the link remains if the command is renamed), which is the currently invoked object by default.

::: {.info -version="TIP478"} Similarly, the object on which the method is invoked via fBmyclassfR is the object that is the current class of the object that owns the namespace that the fBmyclassfR command is contained in initially. As with fBmyfR, the link remains even if the command is renamed into another namespace, and defaults to being the manufacturing class of the current object. :::

Each object has its own fBmyfR and fBmyclassfR commands, contained in its instance namespace.

Examples

This example shows basic use of fBmyfR to use the fBvariablesfR method of the fBoo::objectfR class, which is not publicly visible by default:

oo::class create c {
    method count {} {
        my variable counter
        puts [incr counter]
    }
}

c create o
o count              \(-> prints "1"
o count              \(-> prints "2"
o count              \(-> prints "3"

This example shows how you can use fBmyfR to make callbacks to private methods from outside the object (from a fBtracefR), using fBnamespace codefR to enter the correct context. (See the fBcallbackfR command for the recommended way of doing this.)

oo::class create HasCallback {
    method makeCallback {} {
        return [namespace code {
            my Callback
        }]
    }

    method Callback {args} {
        puts "callback: $args"
    }
}

set o [HasCallback new]
trace add variable xyz write [$o makeCallback]
set xyz "called"     \(-> prints "callback: xyz {} write"

::: {.info -version="TIP478"} This example shows how to access a private method of a class from an instance of that class. (See the fBclassmethodfR declaration in fBoo::definefR for a higher level interface for doing this.) :::

oo::class create CountedSteps {
    self {
        variable count
        method Count {} {
            return [incr count]
        }
    }
    method advanceTwice {} {
        puts "in [self] step A: [myclass Count]"
        puts "in [self] step B: [myclass Count]"
    }
}

CountedSteps create x
CountedSteps create y
x advanceTwice       \(-> prints "in ::x step A: 1"
                     \(-> prints "in ::x step B: 2"
y advanceTwice       \(-> prints "in ::y step A: 3"
                     \(-> prints "in ::y step B: 4"
x advanceTwice       \(-> prints "in ::x step A: 5"
                     \(-> prints "in ::x step B: 6"
y advanceTwice       \(-> prints "in ::y step A: 7"
                     \(-> prints "in ::y step B: 8"