self(3tcl)
π self β Current Method Call Introspection π
ok bestie, this is self(3tcl) from the TclOO Commands crew, and honestly? it's the moment. let's get into it π«Ά
β¨ NAME β¨
self β current method call introspection, aka "who am I and how did I even get here" energy π
πΈ SYNOPSIS πΈ
package require tcl::oo
self ?subcommand?
π DESCRIPTION π
okay so the self command is literally the most main-character thing in TclOO, and it should only be used from inside the context of a call to a method β like, inside a method body, a constructor body, or a destructor body. anywhere else and it's giving lost. π
what it does is let the method find out stuff about how it got called. it takes one argument, subcommand, which tells it what kind of information you actually want. and if you just... don't pass one? the result is the same as if you'd run self object. easy. the subcommands it supports are:
self callβ returns a two-element list describing the method implementations used to implement the current call chain. the first element is the same as whatinfo object callwould report for the current method β except that it also reports actually useful values from inside constructors and destructors, whose names get reported as<constructor>and<destructor>respectively, and for private methods, which get described as beingprivateinstead of beingamethod π (yes, that footnote-style distinction is real and matters). the second element is an index into that first element's list, and it tells you which actual implementation is currently executing β and note the first implementation to execute is always at index0.self callerβ when the method got invoked from inside another object method, this subcommand returns a three-element list describing the containing object and method. element one describes the declaring object or class of the method, element two is the name of the object on which the containing method was invoked, and element three is the name of the method (with the strings<constructor>and<destructor>indicating constructors and destructors respectively). π§self classβ returns the name of the class that the current method was defined within. heads up tho: this will change as the chain of method implementations gets traversed withnext, and if the method was defined on an object, then this will fail. π
if you actually want the class of the current object, that's a whole different construct, bestie:
info object class [self object]
self filterβ when invoked inside a filter, this returns a three-element list describing the filter. element one gives the name of the object or class that declared the filter (note that this may be different from the object or class that provided the implementation of the filter β sneaky β¨), element two is eitherobjectorclassdepending on whether the declaring entity was an object or a class, and element three is the name of the filter.self methodβ returns the name of the current method, with the strings<constructor>and<destructor>indicating constructors and destructors respectively. simple, iconic. πself namespaceβ returns the name of the unique namespace of the object that the method was invoked upon.self nextβ when invoked from a method that is not at the end of a call chain (like, where thenextcommand will actually invoke a real method implementation), this returns a two-element list describing the next element in the method call chain: element one is the name of the class or object that declares the next part of the call chain, and element two is the name of the method (with<constructor>and<destructor>indicating constructors and destructors respectively). and if you invoke it from a method that is at the end of a call chain? it returns the empty string. nothing. silence. π¦self objectβ returns the name of the object that the method was invoked upon. that's it, that's the whole vibe.self targetβ when invoked inside a filter implementation, this returns a two-element list describing the method being filtered. element one will be the name of the declarer of the method, and element two will be the actual name of the method. π
fr fr, self outside a method/constructor/destructor body is not the move. it's only for in-method introspection. periodt.
π EXAMPLES π
this one shows basic use of self to provide info about the current object:
oo::class create c {
method foo {} {
puts "this is the [self] object"
}
}
c create a
c create b
a foo β prints "this is the ::a object"
b foo β prints "this is the ::b object"
and this next one demonstrates what a method call chain looks like, plus how traversing along it changes the index into it. watch it go π₯:
oo::class create c {
method x {} {
puts "Cls: [self call]"
}
}
c create a
oo::objdefine a {
method x {} {
puts "Obj: [self call]"
next
puts "Obj: [self call]"
}
}
a x β Obj: {{method x object method} {method x ::c method}} 0
β Cls: {{method x object method} {method x ::c method}} 1
β Obj: {{method x object method} {method x ::c method}} 0
π SEE ALSO π
info(3tcl), next(3tcl) β go read those too, you'll be obsessed π
π KEYWORDS π
call, introspection, object
and that's the whole page bestie, no cap π this little gem is from TclOO, version 0.1, dated self(3tcl) β catch you in the next method call, slay! β¨ππ¦
π¦ 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