Posted to tcl by cchase at Fri May 27 19:34:20 GMT 2022view raw
- oo::class create Person {
- variable Name Age
- method MethodChaining {args} {
- lassign [self target] className methodName
- puts "$className :: $methodName"
- lassign [info class definition $className $methodName] methodArgs
- if {[llength $args] == 0} {
- return [next]
- } elseif {$methodArgs eq "args"} {
- puts "Target method takes unlimited args."
- return [next {*}$args]
- } else {
- set numMethodArgs [llength $methodArgs]
- set targetArgs [lrange $args 0 $numMethodArgs-1]
- set argsToForward [lrange $args $numMethodArgs end]
- puts "targetArgs: $targetArgs"
- puts "argsToForward: $argsToForward"
- next $targetArgs
- if {[llength $argsToForward] != 0} {
- my {*}$argsToForward
- }
- }
- }
- method multi {args} {
- puts "Hello $args"
- }
- method setName {name} {
- puts "Name set to $name"
- set Name $name
- }
- method setAge {age} {
- puts "Age set to $age"
- set Age $age
- }
- method details {} {
- puts "$Name is $Age years old"
- }
- filter MethodChaining
- }
- Person create guy
- guy setName Bob setAge 33 details
- # Breaks with
- # wrong # args: should be "my setAge age"
- # which appear to be due to methods being invoked within a Filter method not
- # invoking the Filter itself?