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