my(3tcl)
π my, myclass β Invoke any method of current object or its class π
okay bestie, welcome to the manual page for my and myclass, straight out of TclOO Commands, section 3tcl. this is the page about how an object's own methods can call other methods β on itself or on its own class. it's giving very "talking about yourself in the third person" but make it object-oriented. π
β¨ SYNOPSIS β¨
package require tcl::oo
my methodName ?arg ...?
myclass methodName ?arg ...?
πΈ DESCRIPTION πΈ
okay so, first things first:
- the
mycommand exists so that methods of objects can invoke methods of the object (or of its class). π«Ά - the
myclasscommand exists so that methods of objects can invoke methods of the current class of the object β treating that class as an object itself. π
and here's the juicy part, bestie: what can methodName actually be? the valid values for methodName are the set of all methods supported by an object and its superclasses, and that includes the ones that are not exported and the private methods of the object or class β as long as you're using my or myclass from inside another method defined by that object or class. so private stuff is fair game in here. no cap. π₯
now, the object that gets the method invoked via my? it's the one that owns the namespace that the my command is contained in initially β and NB, the link stays even if the command gets renamed. by default, that's the currently invoked object. β¨
similarly, the object on which the method is invoked via myclass is the object that is the current class of the object that owns the namespace that the myclass command is contained in initially. and same deal as my: the link remains even if the command is renamed into another namespace, and it defaults to being the manufacturing class of the current object. π
and one more iconic detail: each object has its own my and myclass commands, contained in its instance namespace. every. single. one. π
π¦ EXAMPLES π¦
example one: basic my, and the sneaky variable method π
this one shows basic use of my to reach the variable method of the oo::object class, which β plot twist β 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"
look at her go. three calls, three prints. periodt. π
example two: callbacks to private methods from outside π
this one shows how you can use my to make callbacks to private methods from outside the object (from a trace), using namespace code to enter the correct context. (and btw, see the callback 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"
the way that namespace code makes the context work?? obsessed. π
example three: reaching a class's private method from an instance π
this one shows how to access a private method of a class from an instance of that class. (and psst β see the classmethod declaration in oo::define 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"
notice how x and y share the same count because myclass is hitting the class itself, not the instance? that's the whole vibe of this example and it ate. π
π SEE ALSO π
next(3tcl)oo::object(3tcl)self(3tcl)
π₯ KEYWORDS π₯
methodmethod visibilityobjectprivate methodpublic method
and that's the page, bestie!! π written against TclOO version 0.1, and it lives under the name my(3tcl) β go forth and call your own methods, you main character. π¦β¨πΈ
π¦ where this page comes from
- ubuntu stonking: tcl9.0-doc 9.0.4+dfsg-1
- Copyright (c) 2007 Donal K. Fellows
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms