Which of these is better and why?
button .b
rename .b .b:cmd
proc .b { args } "eval [list WidgetHandler .b] \$args"
or
button .b
rename .b .b:cmd
interp alias {} .b {} WidgetHandler .b
MS prefers the second, for both clarity and performance reasons (no string parsing at run time).
PWQ An alternate for the first would be:
proc .b {args} {uplevel 1 WidgetHandler .b $args}
So that the widget handler has access to the callers namespace et al.
Vince does anyone have performance data on the different approaches? Note that the proc should be:
proc .b {args} {uplevel 1 [list WidgetHandler .b] $args}
for optimal performance, I think (but haven't tested!).
RS Widget handlers in UIs probably don't make a difference if they run a few milliseconds longer. I'd go for the version that is easiest to read. Oh, and the
list grouping for uplevel is not necessary, as both list elements obviously have no whitespace in them...
DKF: In 8.5, you also have to consider:
proc .b {args} {WidgetHandler .b {*}$args}
(I prefer aliases for this though.)
DKF: In 8.6, you can use
tailcall to handle the really tricky cases.
proc .b {args} {tailcall WidgetHandler .b {*}$args "example bonus tricky thing"}
When that's called,
WidgetHandler will not see the stack frame associated with
.b (indeed, it will be gone entirely).