Artifact 1020d4b9f8113e043fe66421e6f515329ad158c1fc84a0e5627bd5ac06dea2f4:
- File doc/markdown/callback.md — part of check-in [58d4e94c67] at 2025-12-08 08:59:04 on branch tip-700 — - made parseBlock a bit less agressive regarding METHOD/OPTION detection - expanded parseCommand to distinguish between a command and a literal - use camelCase in "global" man page - unified syntax in array/dict/interp/library/unicode man pages, partly also adapted wording - added missing ? in oo::define/info - corrected errors in the encoding man page - updates to the syntax in the DESCRIPTION sections: unified syntax in after/chan/clock/encoding/info/interp - skip converting Tcl-level generated commands in RegConfig.3 to custom markdown as they are non-standard commands (user: Torsten, size: 2734) [annotate] [blame] [check-ins using]
CommandName: callback ManualSection: n Version: 0.3 TclPart: TclOO TclDescription: TclOO Commands Links:
- chan(n)
- fileevent(n)
- my(n)
- self(n)
- socket(n)
- trace(n) Keywords:
- callback
- object Copyright:
- Copyright (c) 2018 Donal K. Fellows ---
Name
callback, mymethod - Generate callbacks to methods
Synopsis
::: {.synopsis} ::: [package]{.cmd} [require]{.sub} [tcl::oo]{.lit}
[callback]{.cmd} [methodName]{.arg} [arg]{.optdot} [mymethod]{.cmd} [methodName]{.arg} [arg]{.optdot} :::
Description
The callback command, also called mymethod for compatibility with the ooutil and snit packages of Tcllib, and which should only be used from within the context of a call to a method (i.e. inside a method, constructor or destructor body) is used to generate a script fragment that will invoke the method, methodName, on the current object (as reported by self) when executed. Any additional arguments provided will be provided as leading arguments to the callback. The resulting script fragment shall be a proper list.
Note that it is up to the caller to ensure that the current object is able to handle the call of methodName; this command does not check that. methodName may refer to any exported or unexported method, but may not refer to a private method as those can only be invoked directly from within methods. If there is no such method present at the point when the callback is invoked, the standard unknown method handler will be called.
Example
This is a simple echo server class. The callback command is used in two places, to arrange for the incoming socket connections to be handled by the Accept method, and to arrange for the incoming bytes on those connections to be handled by the Receive method.
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]
}
}
}