self(n)

πŸ’… self(n) β€” TclOO Commands ✨

okay bestie, so this is the manual page for self, a TclOO command, and honestly? she's the main character of method introspection. no cap.


🌸 NAME 🌸

self β€” method call internal introspection πŸͺž


✨ SYNOPSIS ✨

package require tcl::oo

self ?subcommand?

πŸŽ€ DESCRIPTION πŸŽ€

okay so the self command β€” and listen, she is ONLY allowed to be used from inside the context of a call to a method (like, inside a method body, a constructor body, or a destructor body, periodt) β€” is what lets a method figure out info about how it got called. it's giving detective energy πŸ”

she takes one argument, subcommand, and that's what tells her what kind of info you actually want. and if you just… don't pass it? then the result is the same as if you'd invoked self object. easy πŸ’–

the supported subcommands 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 this one also reports useful values from inside constructors and destructors, whose names get reported as <constructor> and <destructor> respectively, and for private methods, which are described as being private instead of being a method 🫣). the second element is an index into the first element's list that says which actual implementation is currently executing β€” and the first implementation to execute is always at index 0. periodt.
  • 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. the first element describes the declaring object or class of the method, the second element is the name of the object on which the containing method was invoked, and the third element is the name of the method (with the strings <constructor> and <destructor> indicating constructors and destructors respectively). so like, who called me and where πŸ’…
  • self class β€” this returns the name of the class that the current method was defined within. but heads up: this will change as the chain of method implementations is traversed with next, and if the method was defined on an object then this will fail 😭 (not her fault, it's just how it is).

and if you actually want the class of the current object, you need to use this other construct instead:

info object class [self object]
  • self filter β€” when invoked inside a filter, this subcommand returns a three element list describing the filter. the first element gives the name of the object or class that declared the filter (and ngl, this may be different from the object or class that provided the implementation of the filter πŸ‘€), the second element is either object or class depending on whether the declaring entity was an object or a class, and the third element is the name of the filter.
  • self method β€” this returns the name of the current method (with the strings <constructor> and <destructor> indicating constructors and destructors respectively). hi, it's me, your method 🫢
  • self namespace β€” this returns the name of the unique namespace of the object that the method was invoked upon. her own little pocket dimension ✨
  • self next β€” when invoked from a method that is not at the end of a call chain (i.e. where the next command will invoke an actual method implementation), this subcommand returns a two element list describing the next element in the method call chain. the first element is the name of the class or object that declares the next part of the call chain, and the second element is the name of the method (with the strings <constructor> and <destructor> indicating constructors and destructors respectively). but if you invoke it from a method that is at the end of a call chain, this subcommand returns the empty string. that's it, nothing left, no more chain, bye πŸ’€
  • self object β€” this returns the name of the object that the method was invoked upon. the classic. the default. the moment. πŸ‘‘
  • self target β€” when invoked inside a filter implementation, this subcommand returns a two element list describing the method being filtered. the first element will be the name of the declarer of the method, and the second element will be the actual name of the method. lowkey obsessed with how specific she is 🎯

🌈 EXAMPLES 🌈

this example 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 one demonstrates what a method call chain looks like, and how traversing along it changes the index into it:

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 how the index just vibes its way from 0 to 1 and back? slay. πŸ”₯


πŸ‘€ SEE ALSO πŸ‘€

info(n), next(n)


πŸ”‘ KEYWORDS πŸ”‘

call, introspection, object


ok so that's the whole page bestie, go forth and introspect πŸ¦‹

and one last lil sign-off β€” this page is from TclOO, version 0.1, and she's literally just self(n). that's the footer, that's the lore, mwah πŸ’œ

πŸ“¦ where this page comes from