my(n)
πΈ my(n) β TclOO Commands, but make it make sense π
okay bestie, so this is my and myclass from TclOO. two little commands that let a method talk to its own object. lowkey iconic. let's go β¨
β¨ NAME β¨
my, myclass β invoke any method of the current object or its class π«Ά
β¨ SYNOPSIS β¨
package require tcl::oo
my methodName ?arg ...?
myclass methodName ?arg ...?
β¨ DESCRIPTION β¨
so here's the tea. the my command exists so that methods of objects can invoke methods of the object itself (or its class, iykyk). and myclass is the sibling β it lets methods of objects invoke methods of the current class of the object, and it does that by treating the class as an object π¦
the way that this hits: the set of valid values for methodName is literally the set of all methods supported by an object and its superclasses. that includes the ones that aren't exported, and the private methods of the object or class when you use them from inside another method defined by that object or class π
now the important part, no cap:
the object that the method gets invoked on via my is the one that owns the namespace that the my command is contained in initially (NB: the link stays even if the command gets renamed). and by default, that's the currently invoked object π
same energy for myclass: 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. as with 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 every single object has its own my and myclass commands, chillin' inside its instance namespace π
β¨ EXAMPLES β¨
πΈ example one: basic my
this one shows basic use of my to reach the variables method of the oo::object 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"
π example two: callbacks to private methods
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. (see the callback command for the recommended way of doing this, bestie π«Ά)
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"
π example three: private class method from an instance
this one shows how to access a private method of a class from an instance of that class. (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"
β¨ SEE ALSO β¨
β¨ KEYWORDS β¨
- method
- method visibility
- object
- private method
- public method
so yeah, that's the whole thing. my and myclass β small commands, main character energy, absolutely carrying TclOO on their backs π§
and this little baddie is from the TclOO source, version 0.1, filed under my(n) β periodt πβ¨πΈ
π¦ where this page comes from
- fedora 43: tcl-doc 1:9.0.2-1.fc43 Β· TCL AND GPL-3.0-or-later WITH Bison-exception-2.2 AND BSD-3-Clause
- fedora 44: tcl-doc 1:9.0.2-1.fc44 Β· TCL AND GPL-3.0-or-later WITH Bison-exception-2.2 AND BSD-3-Clause
- 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