Prev: expect and stdout
Next: Scrolling in tile
From: iu2 on 22 Oct 2009 01:54 Hi, I started to learn TclOO. How can I retrieve a member variable value from an object? Thanks
From: Donal K. Fellows on 22 Oct 2009 04:58 On 22 Oct, 06:54, iu2 <isra...(a)elbit.co.il> wrote: > I started to learn TclOO. How can I retrieve a member variable value > from an object? What does "retrieve" mean to you? In the simplest alternative (i.e., you want to read the current value of the variable) you just write a trivial method: method foo {} {return $foo} This is because TclOO is actually fairly strict about making fields (which map to variables in an instance-specific namespace) part of the implementation of an object and not part of the interface. In most object systems, this is considered to be good practice anyway. Of course, what I'd actually do instead of the above is... method foo {{value {}}} { if {[llength [info level 0]] == 3} {set foo $value} return $foo } But that's using some more sophisticated techniques (specifically optional arguments call introspection with [info level]...) Donal.
From: iu2 on 22 Oct 2009 06:50 On Oct 22, 10:58 am, "Donal K. Fellows" <donal.k.fell...(a)manchester.ac.uk> wrote: > On 22 Oct, 06:54, iu2 <isra...(a)elbit.co.il> wrote: > > > I started to learn TclOO. How can I retrieve a member variable value > > from an object? > > What does "retrieve" mean to you? In the simplest alternative (i.e., > you want to read the current value of the variable) you just write a > trivial method: > > method foo {} {return $foo} > > This is because TclOO is actually fairly strict about making fields > (which map to variables in an instance-specific namespace) part of the > implementation of an object and not part of the interface. In most > object systems, this is considered to be good practice anyway. > > Of course, what I'd actually do instead of the above is... > > method foo {{value {}}} { > if {[llength [info level 0]] == 3} {set foo $value} > return $foo > } > > But that's using some more sophisticated techniques (specifically > optional arguments call introspection with [info level]...) > > Donal. Thanks for the explanation and example. I noticed that variables don't pass with inheritance to child classes. Is this for the same reason (fields part of objects)? Does it mean that I need to write getter/setter methods if I want to use a parent class variables in the child class?
From: Donal K. Fellows on 22 Oct 2009 08:11 On 22 Oct, 11:50, iu2 <isra...(a)elbit.co.il> wrote: > I noticed that variables don't pass with inheritance to child classes. > Is this for the same reason (fields part of objects)? The variables themselves do as they are present in the instance's pocket namespace, but the declarations (which make the variable automatically available to methods, constructors & destructors declared on the class) don't. This example might help: oo::class create Foo { variable x method setX value {set x $value} } oo::class create Bar { superclass Foo variable x method getX {} {return $x} } Bar create demo demo setX 42 puts "demo has x=[demo getX]" # We can break down the walls of the abstraction a bit more... puts "demo has x=[set [info object namespace demo]::x]" The last line in the example shows that the variable really is just a normal Tcl variable (so you can use Tcl's extensive existing variable handling machinery). It's just got clever syntax wrapped around it. Note also that objects have methods 'varname' and 'variable' (they're not exported to the public API by default, so use through [my]) to allow for manipulation of the same space of variables. > Does it mean that I need to write getter/setter methods if I want to > use a parent class variables in the child class? As shown above, no. Whether or not you use accessors anyway... well, it's up to you. (FWIW, when I was designing the 'variable' declaration it was discussed a lot whether variables declared on a class ought to be isolated - probably by clever renaming - from those declared on a subclass. I went for the current mechanism because it is simpler; fewer odd cases for everyone to understand...) Donal.
|
Pages: 1 Prev: expect and stdout Next: Scrolling in tile |