callback(3tcl)

πŸ“¦
version 9.0.4 Β· 3 versions
, in ubuntu stonking

πŸ’– callback β€” bestie, let's generate some callbacks πŸ’–

okay so this is callback(3tcl) from the TclOO Commands manual, and it's genuinely one of the most useful little glue commands in the whole OO toolkit. sit down, grab a strawberry πŸ“, and let's get into it.


✨ NAME ✨

callback, mymethod β€” generate callbacks to methods. yes, two names, one vibe. slay.


🌸 SYNOPSIS 🌸

package require tcl::oo

callback methodName ?arg ...?
mymethod methodName ?arg ...?

πŸ¦‹ DESCRIPTION πŸ¦‹

The callback command β€” which also answers to mymethod, because it's kept around for compatibility with the ooutil and snit packages of Tcllib β€” is your go-to for turning a method name into a script fragment you can hand off to literally anything that takes a script. πŸ’…

⚠️ real talk: it should only be used from within the context of a call to a method. that means inside a method body, a constructor body, or a destructor body. nowhere else, bestie. it simply doesn't have the context it needs otherwise.

What does it actually do? It generates a script fragment that, when executed, will invoke the method methodName on the current object β€” the one reported by self. 🫢

Any additional arguments you pass in will be handed over as leading arguments</> to that callback. and the resulting script fragment? it shall be a proper list. periodt. Tcl guarantees the shape, so you can pass it straight into after, fileevent, chan event, whatever your heart desires.

Now the important caveats, no cap:

  • It is up to the caller to make sure the current object can actually handle the call of methodName. this command does not check that for you. πŸ™ˆ it trusts you. don't break its trust.
  • methodName may refer to any exported or unexported method. yes, unexported is fine, go off. but it may not refer to a private method β€” those can only be invoked directly from within methods, and that's just the rules. πŸ’€
  • If there's no such method present at the point when the callback is invoked, the standard unknown method handler will be called. so it won't explode silently, but it will go through the normal unknown path.

πŸŽ€ EXAMPLE πŸŽ€

This is a simple echo server class. the callback command shows up in two places here: once to arrange for incoming socket connections to be handled by the Accept method, and once to arrange for the incoming bytes on those connections to be handled by the Receive method. iconic behaviour honestly.

oo::class create EchoServer {
    variable server clients
    constructor {port} {
        set server [socket -server [callback Accept] $port]
        set clients {}
    }
    destructor {
        chan close $server
        foreach client [dict keys $clients] {
            chan close $client
        }
    }

    method Accept {channel clientAddress clientPort} {
        dict set clients $channel [dict create \
                address $clientAddress port $clientPort]
        chan event $channel readable [callback Receive $channel]
    }
    method Receive {channel} {
        if {[chan gets $channel line] >= 0} {
            my echo $channel $line
        } else {
            chan close $channel
            dict unset clients $channel
        }
    }

    method echo {channel line} {
        dict with clients $channel {
            chan puts $channel \
                    [format {[%s:%d] %s} $address $port $line]
        }
    }
}

πŸ‘€ SEE ALSO πŸ‘€

the girlies you should also go read:


⭐ KEYWORDS ⭐

callback, object


okay that's the whole page bestie, go forth and generate callbacks responsibly. this was TclOO version 0.3, callback(3tcl), and yes i'm signing off with a little wave πŸ‘‹βœ¨ mwah, study up, and remember: proper list or it didn't happen. πŸ’–

πŸ“¦ where this page comes from