\documentclass[]{article}
\usepackage{amsmath,graphicx,supertabular,hyperref,tabularx,ifthen}
\title{TIP \#380: TclOO Slots for Flexible Declarations}
\date{October 20, 2010}
\author{Donal K. Fellows}
\urlstyle{sf}
\setlength{\parskip}{1ex}
\setlength{\parindent}{0pt}
\def\tipversion$#1${\texttt{\$#1\$}}
\def\tiplangle#1{\ensuremath{<}}
\def\tiprangle#1{\ensuremath{>}}
\def\tipbar#1{\ensuremath{|}}
\def\tipmail#1#2{\(\langle\){\small\expandafter\url{#1@#2}}\(\rangle\)}
\ifx\pdfoutput\undefined
\newcommand{\tipimage}[2]{\typeout{Make sure you download #1.eps}\ifthenelse{\lengthtest{0.8\textwidth>#2}\and\lengthtest{0pt<#2}}{\includegraphics{#1.eps}}{\includegraphics[width=0.8\textwidth]{#1.eps}}}
\newcommand{\tipxref}[1]{}
\newcommand{\tipxrefend}{}
\else
\newcommand{\tipimage}[2]{\typeout{Make sure you create #1.pdf}\ifthenelse{\lengthtest{0.8\textwidth>#2}\and\lengthtest{0pt<#2}}{\includegraphics{#1.pdf}}{\includegraphics[width=0.8\textwidth]{#1.pdf}}}
\pdfcatalog{/PageMode /UseOutlines}
\newcommand{\tipxref}[1]{\pdfannotlink attr {/C [0.5 0.5 1.0] /Border [0 0 1]} goto name {#1}}
\newcommand{\tipxrefend}{\pdfendlink}
\fi
\newenvironment{tipabstract}{\begin{abstract}}{\end{abstract}}
\begin{document}\maketitle
\begin{center}\begin{tabularx}{\linewidth}{|r@{: }X|}\hline
\textbf{TIP \#380}&\textbf{TclOO Slots for Flexible Declarations}\\\hline
Author&

Donal K. Fellows \tipmail{dkf}{users.sf.net}
\\
Created&Wednesday, \(\text{20}^{\text{th}}\) October 2010\\
Type&Project\\
Tcl Version&\(8.6\)\\
State&Final\\
Vote&Done\\
Version&\tipversion$Revision: 1.5 $\\
Post History&\\
\hline\end{tabularx}\end{center}
\thispagestyle{empty}\pagestyle{empty}
\begin{tipabstract}

This TIP proposes a system that enables a more flexible system of handling all declarations relating to classes and objects that resolve to control over a list of things. This system, which will use TclOO to configure itself, will enable the scripting of additional mechanisms for manipulating declarations and will also include a change to the default behavior of the \textbf{filter} and \textbf{variable} declarations, where the standard ``replace everything'' behavior was found to be non-obvious in practice. In order to enable this, the exact behavior of the \textbf{unknown} method mechanism will be slightly varied.
\end{tipabstract}
\tableofcontents\setcounter{page}{0}\clearpage\pagestyle{plain}

\section{Rationale}

Substantial deployment experience with TclOO now exists, and one of the things that has been consistently found to be surprising has been the way that the \textbf{variable} declaration (part of \textbf{oo::define} and \textbf{oo::objdefine}) replaces the current list of declared variables. Personal experimentation has indicated that the \textbf{filter} declaration is just as surprising, though it is admittedly less frequently used. Therefore it is desirable to alter the way such declarations work so that they append to the list of variables (or filters, depending on what is being declared).

However, if this is being done then there is a need for substantial complexity to be added; there must be \textit{some} way to set the list of things being declared or clear it as well as appending to it. Moreover, at the base level both \textbf{superclass} and \textbf{mixin} declarations are basically the same structure; a list of things. For moral consistency, it becomes necessary to apply the same basic rules there, though with different defaults; in practice the default ``replace'' rule is suitable in for both those two cases.

Note that for the declarations not mentioned, their natural model is not that of a list so there is no underlying unity of declaration system possible at this level. They will be left unchanged by this TIP.

\section{Proposed Change}

My proposed solution is the introduction of a new standard class, \textbf{::oo::Slot}, whose instances will provide configuration services to the \textbf{oo::define} and \textbf{oo::objdefine} commands. The proposed definition of the class will be this:
\begingroup\small\begin{verbatim}
oo::define oo::Slot {
    method Get {} {error unimplemented}
    method Set list {error unimplemented}
    method --default-operation args {error unimplemented}
    method -set args {
        tailcall my Set $args
    }
    method -append args {
        tailcall my Set [list \
                {*}[uplevel 1 [list [namespace which my] Get]] {*}$args]
    }
    method -clear {} {
        tailcall my Set {}
    }
    method unknown {args} {
        if {[llength $args] == 0} {
            tailcall my --default-operation
        } elseif {![string match -* [lindex $args 0]]} {
            tailcall my --default-operation {*}$args
        }
        next {*}$args
    }
    export -set -append -clear
    unexport unknown destroy
}
\end{verbatim}
\endgroup

It will be up to the instances (whose names do \textit{not} form part of this specification) to define appropriate interpretations for the \textbf{Get}, \textbf{Set} and \textbf{-{}-default-operation} methods, with the last being envisaged as an ideal fit for being done by one of the other methods, with the connection being made by \textbf{forward}.

In order to enable this, a small change is required to the \textbf{unknown} method mechanism. This change is that in the case that there is no method name at all passed to a call into an object, that will have to be handled by passing into the unknown method mechanism. This will only have a significant impact on declarations of the \textbf{unknown} method where the formal argument list was \textit{args}; in the other cases there is typically a required first formal argument and that leads under the current implementation of methods to the same error message as currently defined. Since this is a rare case of a rarely used mechanism, I judge that this is likely to be an acceptable semantic change.

The real major change here is that for certain things (i.e., \textbf{variable}, \textbf{superclass}, \textbf{mixin} and \textbf{filter} declarations) it becomes trickier to use these with named things whose names start with a ``-'' character. I believe this to be a reasonable and minimal trade-off, given that in return we are getting more natural usage for many users while maintaining a standard mechanism for a whole way of handling declarations.

\section{Reference Implementation}

See SF Patch \#3084339 [\url{https://sourceforge.net/support/tracker.php?aid=3084339}]

\section{Copyright}

This document has been placed in the public domain.

\section{Colophon}

\textit{TIP AutoGenerator --- written by Donal K. Fellows}
\end{document}
