Posted to tcl by colin at Fri Feb 27 12:47:43 GMT 2009view raw

  1. package require TclOO
  2. namespace import oo::*
  3.  
  4. class create test {
  5. variable var
  6. method froopy {args} {puts stderr "called from coro $args"}
  7. method start {} {
  8. puts stderr "start ns:[namespace current] self:[self]"
  9.  
  10. proc aproc {args} {
  11. puts stderr "$args a proc defined in [namespace current] from coro [info coroutine]"
  12. }
  13.  
  14. puts stderr "Commands prior to coro: [info commands [namespace current]::*]"
  15.  
  16. coroutine moop ::apply [list args {
  17. variable var
  18. aproc Calling
  19. proc bproc {args} {
  20. puts stderr "$args b proc defined in [namespace current] from coro [info coroutine]"
  21. }
  22. bproc Calling
  23. puts stderr "Commands in object ns after coro: [info commands [namespace current]::*]"
  24. puts stderr "Vars in object ns: [info vars [namespace current]::*]"
  25. puts stderr "var: $var"
  26. while {1} {
  27. set args [yield MOOP]
  28. puts stderr "Coro [info coroutine] called with '$args'" ;# show sign of being called
  29. my froopy with 'my froopy' ;# call a method from the coro
  30. }
  31. } [self]]
  32.  
  33. return [self]::moop
  34. }
  35.  
  36. constructor {} {
  37. set var "this var (v) can be seen from the coro via 'variable' command"
  38. }
  39. }
  40.  
  41. set x [test new] ;# create the test object
  42. set coro [$x start] ;# call start to generate the coro
  43. $coro "calling coro as $coro" ;# call the coro
  44. ${x}::moop "calling coro explicit '${x}::moop'" ;# alternative way to call coro
  45.  
  46. Results:
  47.  
  48. start ns:::oo::Obj4 self:::oo::Obj4
  49. Commands prior to coro: ::oo::Obj4::aproc ::oo::Obj4::my
  50. Calling a proc defined in ::oo::Obj4 from coro ::oo::Obj4::moop
  51. Calling b proc defined in ::oo::Obj4 from coro ::oo::Obj4::moop
  52. Commands in object ns after coro: ::oo::Obj4::aproc ::oo::Obj4::bproc ::oo::Obj4::my ::oo::Obj4::moop
  53. Vars in object ns: ::oo::Obj4::var
  54. var: this var (v) can be seen from the coro via 'variable' command
  55. Coro ::oo::Obj4::moop called with 'calling coro as ::oo::Obj4::moop'
  56. called from coro with 'my'
  57. Coro ::oo::Obj4::moop called with 'calling coro explicit '::oo::Obj4::moop''
  58. called from coro with 'my'
  59. colin@thighbone:~/Desktop/Work/Wub/Client$ !!
  60. tclsh test3.tcl
  61. start ns:::oo::Obj4 self:::oo::Obj4
  62. Commands prior to coro: ::oo::Obj4::aproc ::oo::Obj4::my
  63. Calling a proc defined in ::oo::Obj4 from coro ::oo::Obj4::moop
  64. Calling b proc defined in ::oo::Obj4 from coro ::oo::Obj4::moop
  65. Commands in object ns after coro: ::oo::Obj4::aproc ::oo::Obj4::bproc ::oo::Obj4::my ::oo::Obj4::moop
  66. Vars in object ns: ::oo::Obj4::var
  67. var: this var (v) can be seen from the coro via 'variable' command
  68. Coro ::oo::Obj4::moop called with 'calling coro as ::oo::Obj4::moop'
  69. called from coro with 'my froopy'
  70. Coro ::oo::Obj4::moop called with 'calling coro explicit '::oo::Obj4::moop''
  71. called from coro with 'my froopy'

Comments

Posted by colin at Fri Feb 27 12:53:30 GMT 2009 [text] [code]

on reflection, I think the line above: return [self]::moop

Posted by colin at Fri Feb 27 12:55:13 GMT 2009 [text] [code]

on reflection, I think the line above: return [self]::moop would be safer as: return [namespace current]::moop, as it's nowhere guaranteed that [namespace current] eq [self] in a method invocation, but it is guaranteed that there will be a namespace (obviously enough)

Posted by dkf at Fri Feb 27 13:14:36 GMT 2009 [text] [code]

Can also use [self namespace]