Updated 2013-01-08 13:49:06 by dkf

jima (2013-01-08) notices a useful interaction between objects of a class and mixins in TclOO regarding how variables are accesed.

Example code:
 oo::class create Class {
  variable var
  constructor {} {
   set var wea
   oo::objdefine [self] mixin Mixin
  }
 }

 oo::class create Mixin {
  variable var
  method read {} {
   puts VAR($var)
  }
  method write {val} {
   set var $val
  }
 }

 Class create c
 c read
 c write foo
 c read
 c write bar
 c read

The interesting bit is that one can assign dynamically different mixins to a instance of a class and use in the code of the methods of the mixin variables that have been "declared" with variable both in the class and in the mixin.

To me it wasn't trivial that both var references in the example code would resolve to the same underlying variable.

DKF: We debated having different variables for each class (through magical name rewriting or something like that) but I declined to implement on the grounds that it was much more complicated to explain. Right now, all variable names are just the names of normal Tcl variables in the instance's namespace: that's a trivial rule. It only really becomes a problem with a deep class hierarchy, but that is usually a bad idea anyway (and composition/delegation should be favored over inheritance most of the time).