Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Curious problem with tclOO

90 views
Skip to first unread message

Michael Keith

unread,
May 30, 2016, 10:27:53 AM5/30/16
to
Hello everybody,
I ran into a problem I don't understand. If I call
t count 100
it puts out
info vars ::t: maxVal_ clkIn_ numBits_ clkOut_ val_
which (to me) shows that maxVal_ is known, but accessing it in the
next line it says
can't read "maxVal_": no such variable

Huh? And why doesn't it complain about the line just above
incr val_ $cnt

It would be very kind if somebody could shed some light on this.
Thanks
M'
------
catch {BaseReg destroy}

#
# The base class of our registers
#
oo::class create BaseReg {
variable val_ numBits_ maxVal_

constructor {} {
set val_ 0
set numBits_ 8
set maxVal_ [expr {2 ** $numBits_}]
trace add variable numBits_ write [list [self] setMaxVal]
}

# A poor man's get/set method
method set {args} {
set name [lindex $args 0]
if {[llength $args] == 2} {
set {*}$args
}
return [set $name]
}

method setMaxVal {args} {
set maxVal_ [expr {2 ** $numBits_}]
}
}

#
# A basic timer
#
oo::class create BasicTimer {
superclass BaseReg
variable clkIn_ clkOut_

constructor {} {
next
set clkIn_ "" ; set clkOut_ ""
}

# count
method count {cnt} {
puts "info vars [self]: [info object vars [self]]"
incr val_ $cnt
set outCnt [expr {$val_ / $maxVal_}]
set val_ [expr {$val_ % $maxVal_}]
if {$clkOut_ ne ""} {
$clkOut_ count $outCnt
}
}
}

BasicTimer create t
t count 100
------

Michael Keith

unread,
May 30, 2016, 12:34:35 PM5/30/16
to
Answering my own question:
I was missing the statement
my variable val_ maxVal_
in the method 'count'.

What probably led me astray was that
info object vars [self]
returned _all_ variables: those from the base class as well as from
the derived class.

O well, all is good now and I am back on track.
M'

Donal K. Fellows

unread,
Jun 1, 2016, 5:38:50 AM6/1/16
to
On 30/05/2016 17:34, Michael Keith wrote:
> What probably led me astray was that
> info object vars [self]
> returned_all_ variables: those from the base class as well as from
> the derived class.

It delivers (the names of) the variables in the object; they're just
variables in the object's instance namespace, nothing more. There's no
attempt to make variables defined by different classes separate, in part
because there are quite reasonable cases where you might want to have
subclasses and superclasses collaborating around a variable.

The only variables visible in a method (other than by the usual schemes
you use with a procedure) are those that you have asked for with the
[variables] *declaration* in the method's declaration scope. The
declaration scope of a method is its enclosing class (or the instance
for a pure instance method).

oo::class create Foo {
variable bar
method boo {} {
incr bar
}
}

The variable itself resides in the instance, but the request to make it
visible is in the class (as a directive to the variable resolution
engine, which is one of the deeply scary bits of Tcl's implementation).

Donal.
--
Donal Fellows — Tcl user, Tcl maintainer, TIP editor.

Michael Keith

unread,
Jun 2, 2016, 7:24:52 AM6/2/16
to
Hi Donal,
thanks for the explanation.

I'm not quite sure I understood all details but it helped nonetheless.

>engine, which is one of the deeply scary bits of Tcl's implementation).
I trust you with this :)

Thanks again
M'
0 new messages