TclOO: questions (Method Chaining, next, my variable)
- From: eiji <Eiji@xxxxxxxxxxxxxxxx>
- Date: Tue, 30 Oct 2007 04:14:31 -0700
I moved them from "Ask, and it shall be given #6":
Actually I have three questions regarding "Method Chaining", "next"
and "my variable".
Method Chaining:
In the TIP #257 [http://www.tcl.tk/cgi-bin/tct/tip/257.html] you find
information about the so called "Method-Chaining", and I played a
little bit with that feature and determine, that there is no statement
to when the forward-declaration is called.
In the following example I see that the filters and mixings intercept
before the forward proc. I that well defined behavior?
example:
package require TclOO
oo::class create mixinbase {
constructor {} {
puts "mixinbase::constructor"
}
method foo {} {
puts "mixinbase::foo"
next
}
}
oo::class create mixinderived {
constructor {} {
puts "mixinderived::constructor"
}
method foo {} {
puts "mixinderived::foo"
next
}
}
oo::class create base {
constructor {} {
puts "base::constructor"
}
method foo {} {
puts "base::foo"
}
method filter {} {
puts "base::filter"
next
}
filter filter
mixin mixinbase
forward foo testperclass; #output 1
}
oo::class create derived {
constructor {} {
puts "derived::constructor"
}
method foo {} {
puts "derived::foo"
next
}
method filter {} {
puts "derived::filter"
next
}
filter filter
mixin mixinderived
#forward foo testperclass; #output 2
}
proc testperclass {args} {
puts "proc testperclass"
}
proc testperobject {args} {
puts "proc testperobject"
}
oo::define derived superclass base
set o [derived new]
$o foo
oo::define $o forward foo testperobject
$o foo
output 1:
derived::constructor
derived::filter
base::filter
mixinderived::foo
mixinbase::foo
derived::foo
proc testperclass
derived::filter
base::filter
mixinderived::foo
mixinbase::foo
proc testperobject
Okay, derived::foo is called and "forward by base" evaluating the
next.
output 2:
derived::constructor
derived::filter
base::filter
mixinderived::foo
mixinbase::foo
proc testperclass
derived::filter
base::filter
mixinderived::foo
mixinbase::foo
proc testperobject
Is there any reason why the mixings intercept before forward?
I was just wondering and some dicussion on TclOO would be interesting
too.
next:
Shouldn't ''next'' on derived throw an error?
package require TclOO
oo::class create base {
constructor {} {
puts "base::constructor"
}
method foo {} {
puts "base::foo"
}
}
oo::class create derived {
constructor {} {
next
puts "derived::constructor"
}
method foo {} {
next
puts "derived::foo"
}
}
# oo::define derived superclass base
set o [derived new]
$o foo
output (no error without defined superclass):
derived::constructor
derived::foo
TIP #257 says:
"It is an error to invoke the next
command when there is no superclass
definition of the current method."
constructor->next is no error because oo::class has one
but oo::class has no foo
FYI (with superclass):
oo::define derived superclass base
set o [derived new]
$o foo
output:
base::constructor
derived::constructor
base::foo
derived::foo
my variable:
What's the difference between ''variable'' and ''my variable''?
package require TclOO
oo::class create test {
constructor {} {
my variable myvar
set myvar 1
variable var 2
}
method foo {} {
puts [my varname myvar]
puts [my varname var]
}
method bar {} {
variable myvar
variable var
puts $myvar
puts $var
}
}
set t [test new]
$t foo
$t bar
puts [info object vars $t]
gives:
::oo::Obj4::myvar
::oo::Obj4::var
1
2
var myvar
What is the benefit using ''my variable''?
In the first place, everything looks similar to ''variable''.
.
- Follow-Ups:
- Re: TclOO: questions (Method Chaining, next, my variable)
- From: Donal K. Fellows
- Re: TclOO: questions (Method Chaining, next, my variable)
- Prev by Date: Re: rotate tk canvas
- Next by Date: Re: tcl - exec will block the main thread if not return on Windows
- Previous by thread: rotate tk canvas
- Next by thread: Re: TclOO: questions (Method Chaining, next, my variable)
- Index(es):