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 what info object call would 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 being private instead of being a method πŸ’… (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 index 0.
  • 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 with next, 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 either object or class depending 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 the next command 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