<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE TIP SYSTEM "http://www.tcl.tk/cgi-bin/tct/tip/tipxml.dtd">
<!-- Converted at Thu May 17 04:15:16 GMT 2012 -->
<!-- TIP AutoGenerator - written by Donal K. Fellows -->

<TIP number='320'>
<header><title>Improved Variable Handling in the Core Object System</title><author address="mailto:dkf@users.sf.net">Donal K. Fellows</author><status type='project' state='final' tclversion="8.6" vote='after'>$Revision: 1.6 $</status><history></history><created day='13' month='jun' year='2008' /><keyword>TclOO</keyword></header>
<abstract>This TIP specifies new configuration commands that allow variables to be used in methods of the core object system without explicit declaration in each method.</abstract>
<body><section title="Rationale">
<para>During the discussions leading up to the vote on <tipref type="text" tip="257"/>, it became apparent that being able to access at least some variables of an object without explicit declaration in each method was desirable. Doing this would make working with the core object system much easier in many common cases, and would also allow for potentially more efficient implementation.</para>
<para>However, there is a balance to be drawn. If every variable of the object was made available in the method by default (the simplest case) there would frequently be problems with interactions between the formal arguments of a superclass&apos;s methods and the instance variables of a subclass! That would be highly undesirable, as it completely breaks the principle of isolation of class implementations.</para>
<para>There are two ways of dealing with this issue.</para>
<enumerate><item.e index='1'><para>Make the variables understood by a particular class be distinct from those understood by subclasses of the class.</para></item.e><item.e index='2'><para>Only bring those variables into scope that are actually declared by a particular class.</para></item.e></enumerate>
<para>Studying the first alternative, there are two ways to actually achieve this: not putting the variables in a namespace but actually using a separate structure (poor, because then we would lose the ability to easily use the variable with many of Tcl&apos;s general variable facilities), or modifying the namespace-visible name of each variable to include some unique string coupled to the class declaring it. Though I think that the second option is better than the first (actual implementations would be able to use the <emph style="bold">oo::object</emph> class&apos;s <emph style="bold">variable</emph> and <emph style="bold">varname</emph> methods to hide many of the details where they are currently exposed at all) I still do not particularly like it since it would mean that the cases where a class and a subclass <emph style="italic">want</emph> to refer to the same variable are very awkward.</para>
<para>Hence I think that there should be a mechanism for declaring what variables a class has, that those names should be the namespace-visible names of the variables, and that only those variables that are declared by a particular class should be <emph style="italic">automatically</emph> visible in the class&apos;s methods (including the constructor and destructor, of course). As a matter of basic symmetry, object-level declarations of variables and methods should also be possible.</para>
</section>
<section title="Proposed Change">
<para>Every object and every class will have associated with it a list of variable names. This list will be configurable via a subcommand of <emph style="bold">oo::define</emph> and introspectable. Methods (strictly, only procedure-like methods, constructors and destructors) will then make those variables that were declared at the same declaration level (i.e. in the same class, or in the object itself for methods defined on the object) available in the method body without further declaration or qualification. At declaration time, the type of the variable (i.e. whether it is an array or a simple variable) will not be defined; that may be done in the constructor. As there is no (public) C API for declaring a procedure-like method, there will not be a public C API for declaring the variables either.</para>
<para>Note that the order in which the declaration of some methods and some variables for a class or object will not matter.</para>
<subsection title="Declaration">
<para>The <emph style="bold">oo::define</emph> and <emph style="bold">oo::objdefine</emph> (and also the <emph style="bold">self</emph> subcommand of <emph style="bold">oo::define</emph>) will gain a new subcommand: <emph style="bold">variable</emph>. This will take zero or more arguments, and those arguments will form the list of variables that are made available by default. Each argument will need to be a valid variable local name without parentheses.</para>
<para>Syntax (other uses analogous according to normal behaviour of the class and object definition commands):</para>
<quote><emph style="bold">oo::define</emph> <emph style="italic">class</emph> <emph style="bold">variable</emph> ?<emph style="italic">varName ...</emph>?</quote>
</subsection>
<subsection title="Introspection">
<para>The list of variables declared by a particular class, <emph style="italic">class</emph>, may be retrieved by:</para>
<quote><emph style="bold">info class variables</emph> <emph style="italic">class</emph></quote>
<para>The list of variables declared by a particular object, <emph style="italic">object</emph>, may be retrieved by:</para>
<quote><emph style="bold">info object variables</emph> <emph style="italic">object</emph></quote>
<para>Note that the full list of <emph style="italic">all</emph> variables (matching an optional pattern) in an object&apos;s namespace will be retrievable using <emph style="bold">info object vars</emph>; this mechanism is disjoint (though <emph style="bold">info object variables</emph> will return variables that can be returned by <emph style="bold">info object vars</emph>).</para>
</subsection>
</section>
<section title="Implementation">
<para><url ref="https://sourceforge.net/support/tracker.php?aid=2005460"/></para>
</section>
<section title="Copyright">
<para>This document has been placed in the public domain.</para>
</section>
</body></TIP>

