my(n)

πŸ“¦
version 9.0.3 Β· 3 versions
, in alpine 3.23, alpine 3.24, nixos 26.05

πŸ’… NAME πŸ’…

Bestie, welcome to the my(n) page from TclOO Commands! ✨ This one is all about my and myclass β€” the little commands that let you invoke any method of the current object or its class. Iconic behavior, honestly. 🫢

✨ SYNOPSIS ✨

package require tcl::oo

my methodName ?arg ...?
myclass methodName ?arg ...?

πŸ¦‹ DESCRIPTION πŸ¦‹

Okay so here's the tea β˜•: my lets methods of objects invoke methods of the object β€” or its class. And myclass lets methods of objects invoke methods of the current class of the object, treating that class as an object. Main character energy for real. πŸ’…

The valid values for methodName are literally the set of all methods supported by an object and its superclasses. That includes methods that are not exported, plus private methods of the object or class β€” but only when you're using them inside another method defined by that object or class. iykyk. πŸ‘€

The object on which the method is invoked via my is the one that owns the namespace that the my command is contained in initially. NB: the link stays put even if the command gets renamed. By default, that's the currently invoked object. Slay. ✨

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. 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. No cap. πŸ¦‹

Each object has its own my and myclass commands, contained in its instance namespace. Periodt. πŸŽ€

🌸 EXAMPLES 🌸

This example shows basic use of my to use 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"

This example 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.) Obsessed with the context switch, ngl. πŸ’œ

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"

This example 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.) Cute, private, and powerful β€” the trifecta. 🧚

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 πŸ‘€

Go peek, bestie. These are the linked lore. πŸ”₯

πŸŽ€ KEYWORDS πŸŽ€

  • method
  • method visibility
  • object
  • private method
  • public method

Source/version: TclOO 0.1 β€” that's the footer tea, fr fr. No date was listed in the original, so I'm not inventing one, bestie. Mwah. πŸ’–πŸ’…

πŸ“¦ where this page comes from