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

package require TclOO
namespace import oo::*

class create test {
    variable var
    method froopy {args} {puts stderr "called from coro $args"}
    method start {} {
	puts stderr "start ns:[namespace current] self:[self]"
	
	proc aproc {args} {
	    puts stderr "$args a proc defined in [namespace current] from coro [info coroutine]"
	}

	puts stderr "Commands prior to coro: [info commands [namespace current]::*]"

	coroutine moop ::apply [list args {
	    variable var
	    aproc Calling
	    proc bproc {args} {
		puts stderr "$args b proc defined in [namespace current] from coro [info coroutine]"
	    }
	    bproc Calling
	    puts stderr "Commands in object ns after coro: [info commands [namespace current]::*]"
	    puts stderr "Vars in object ns: [info vars [namespace current]::*]"
	    puts stderr "var: $var"
	    while {1} {
		set args [yield MOOP]
		puts stderr "Coro [info coroutine] called with '$args'"	;# show sign of being called
		my froopy with 'my froopy'			;# call a method from the coro
	    }
	} [self]]

	return [self]::moop
    }

    constructor {} {
	set var "this var (v) can be seen from the coro via 'variable' command"
    }
}

set x [test new]	;# create the test object
set coro [$x start]	;# call start to generate the coro
$coro "calling coro as $coro"	;# call the coro 
${x}::moop "calling coro explicit '${x}::moop'"	;# alternative way to call coro

Results:

start ns:::oo::Obj4 self:::oo::Obj4
Commands prior to coro: ::oo::Obj4::aproc ::oo::Obj4::my
Calling a proc defined in ::oo::Obj4 from coro ::oo::Obj4::moop
Calling b proc defined in ::oo::Obj4 from coro ::oo::Obj4::moop
Commands in object ns after coro: ::oo::Obj4::aproc ::oo::Obj4::bproc ::oo::Obj4::my ::oo::Obj4::moop
Vars in object ns: ::oo::Obj4::var
var: this var (v) can be seen from the coro via 'variable' command
Coro ::oo::Obj4::moop called with 'calling coro as ::oo::Obj4::moop'
called from coro with 'my'
Coro ::oo::Obj4::moop called with 'calling coro explicit '::oo::Obj4::moop''
called from coro with 'my'
colin@thighbone:~/Desktop/Work/Wub/Client$ !!
tclsh test3.tcl
start ns:::oo::Obj4 self:::oo::Obj4
Commands prior to coro: ::oo::Obj4::aproc ::oo::Obj4::my
Calling a proc defined in ::oo::Obj4 from coro ::oo::Obj4::moop
Calling b proc defined in ::oo::Obj4 from coro ::oo::Obj4::moop
Commands in object ns after coro: ::oo::Obj4::aproc ::oo::Obj4::bproc ::oo::Obj4::my ::oo::Obj4::moop
Vars in object ns: ::oo::Obj4::var
var: this var (v) can be seen from the coro via 'variable' command
Coro ::oo::Obj4::moop called with 'calling coro as ::oo::Obj4::moop'
called from coro with 'my froopy'
Coro ::oo::Obj4::moop called with 'calling coro explicit '::oo::Obj4::moop''
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]