Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

tclOO: identify a METOD as CALLBACK

72 views
Skip to first unread message

Andreas Otto

unread,
Jul 5, 2020, 4:45:14 AM7/5/20
to
Hi,

I want to use a "method" as callback like:
> $ctx ServiceCreate "OTTO" myCallback


BUT I don't know if the "myCallback" is a "method" OR an ordinary "proc"…
In addition I don't know if the method is a:
1) STATIC class method (self method myStaticMethod ...)
2) a superclass class method
3) current (toplevel) class method
4) OR an object method.

# example for a "superclass" method NOT identified by the "toplevel" class

> oo::class create c {
variable counter
method callback {} {
variable counter
incr counter
}
}
::c

> oo::class create cc {
superclass c
}
::cc

> info class methods cc -all
callback destroy

> info class methodtype cc callback
unknown method "callback"
while executing
"info class methodtype cc callback"
("eval" body line 1)
invoked from within
"eval $args"
(procedure "run" line 3)
invoked from within
"run info class methodtype cc callback"


;fg

tombert

unread,
Jul 5, 2020, 12:39:57 PM7/5/20
to
From design perspective it is best you always assume that the callback is on global level i.e. a procedure.

The magic lies in the way you pass on the callback:

# http://wiki.tcl.tk/21595. v20
# @author Donal Fellows
proc ::oo::Helpers::callback {method args} {
list [uplevel 1 {namespace which my}] $method {*}$args
}

oo::class create test {}

oo::define test method sc {cb} {
uplevel "#0" $cb
}

oo::define test method print {} {
puts "this is the method print"
}

oo::define test method docb {} {
my sc [callback print]
}

proc cb1 {args} {puts "this is the proc cb1"}

test create c
c sc cb1
c sc [list c print]
c docb
0 new messages