Sarnold thinks that classes should look - as possible as they can - like namespaces, because it is the current encapsulation model. It may be extreme, but some developers may want to convert quickly a namespace in a Class or Megawidget - while the opposite cannot be as simple. I would like to see:
namespace class foo { common counter variable names proc __init__ {args} { common counter incr counter variable names array set names $args } # it has to look like a normal Tcl proc, so it has an arglist proc __destroy__ {} { common counter incr counter -1 } proc display {} { variable names puts "My name is $names(first) $names(last)" } proc count {} { common counter return $counter } } foo new name first Richard last Heinz name destroy set name [foo %AUTO% first Richard last Heinz] name display set name::names(first) George name display # generates an error, because it is an instance method #foo display $name destroy
Where common would behave like variable, with a static qualifier. I submit that a sequence of variable bar may be replaced by instance command that wraps all variable calls related to (instance) variables.
namespace class foo { variable a variable name variable tool variable logfile proc __init__ ... proc __destroy__ ... proc display {{intologfile no}} { instance ; # no need to invoke 'variable' again if {$intologfile} { puts $logfile "a=$a name=$name tool=$tool" } else { puts "a=$a name=$name tool=$tool" } } ... }
And a proc is a class method, until it invokes variable or instance commands, or uses an instance namespace qualifier. During instanciation, all namespace qualifiers equal to ::foo should be replaced by the instance's namespace qualifier, so that we can still use -textvariable Tk option.
namespace class foo { variable label proc __init__ {w} { label $w.lbl -textvariable ::foo::label pack $w.lbl } ... }
The class name may instantiate objects by two methods:
foo new <instancename> <constructor_args> foo %AUTO% <constructor_args>
Any proc (without use of instance variables) could be called this way :
foo <myproc> <args>
The two special procs,
__init__ __destroy__
should throw an error when they are explicitly called.