Posted to tcl by cchase at Fri May 27 19:34:20 GMT 2022view raw

  1. oo::class create Person {
  2. variable Name Age
  3.  
  4. method MethodChaining {args} {
  5. lassign [self target] className methodName
  6. puts "$className :: $methodName"
  7. lassign [info class definition $className $methodName] methodArgs
  8.  
  9. if {[llength $args] == 0} {
  10. return [next]
  11. } elseif {$methodArgs eq "args"} {
  12. puts "Target method takes unlimited args."
  13. return [next {*}$args]
  14. } else {
  15. set numMethodArgs [llength $methodArgs]
  16.  
  17. set targetArgs [lrange $args 0 $numMethodArgs-1]
  18. set argsToForward [lrange $args $numMethodArgs end]
  19. puts "targetArgs: $targetArgs"
  20. puts "argsToForward: $argsToForward"
  21. next $targetArgs
  22.  
  23. if {[llength $argsToForward] != 0} {
  24. my {*}$argsToForward
  25. }
  26.  
  27.  
  28. }
  29.  
  30. }
  31.  
  32. method multi {args} {
  33. puts "Hello $args"
  34. }
  35.  
  36. method setName {name} {
  37. puts "Name set to $name"
  38. set Name $name
  39.  
  40. }
  41.  
  42. method setAge {age} {
  43. puts "Age set to $age"
  44. set Age $age
  45.  
  46. }
  47.  
  48. method details {} {
  49.  
  50. puts "$Name is $Age years old"
  51. }
  52.  
  53. filter MethodChaining
  54. }
  55.  
  56. Person create guy
  57.  
  58. guy setName Bob setAge 33 details
  59.  
  60. # Breaks with
  61. # wrong # args: should be "my setAge age"
  62. # which appear to be due to methods being invoked within a Filter method not
  63. # invoking the Filter itself?