\documentclass[]{article}
\usepackage{amsmath,graphicx,supertabular,hyperref,tabularx,ifthen}
\title{TIP \#470: Reliable Access to OO Definition Context Object}
\date{April 23, 2017}
\author{Donal 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 \#470}&\textbf{Reliable Access to OO Definition Context Object}\\\hline
Author&

Donal Fellows \tipmail{dkf}{users.sf.net}
\\
Created&Sunday, \(\text{23}^{\text{rd}}\) April 2017\\
Type&Project\\
Tcl Version&\(8.7\)\\
State&Final\\
Vote&Done\\
Version&\tipversion$Revision: 1.4 $\\
Keywords&TclOO, metaprogramming\\
Post History&\\
\hline\end{tabularx}\end{center}
\thispagestyle{empty}\pagestyle{empty}
\begin{tipabstract}

This TIP makes it easier for people to write procedures to extend TclOO's definition sublanguage.
\end{tipabstract}
\tableofcontents\setcounter{page}{0}\clearpage\pagestyle{plain}

\section{Rationale}

One of the fundamental features of Tcl is that you can extend it with more capabilities by writing your own procedures (and other commands, if you prefer the C API). However, it is somewhat awkward to do so when using TclOO, as the \textbf{oo::define} and \textbf{oo::objdefine} commands don't make it easy to find out what the context class or object is.

For example, in the \textit{oo::util} package of Tcllib, the code for discovering what the context class is includes this [\url{http://core.tcl-lang.org/tcllib/artifact/51d71f560ceb7d63?ln=77}]:
\begingroup\small\begin{verbatim}
    # Get the name of the current class or class delegate 
    set cls [namespace which [lindex [info level -1] 1]]
\end{verbatim}
\endgroup

That is ugly, and won't even work reliably for getting the context object in \textbf{oo::objdefine} as that can be entered into by multiple paths (i.e., there's a shortcut from \textbf{oo::define}).

\section{Proposed Change}

I propose to make the existing \textbf{self} command in \textbf{oo::define}, when invoked without arguments, return the context class (provided it is evaluated in the correct stack frame, as usual with definition commands). Similarly, I also propose to add a \textbf{self} command to the \textbf{oo::objdefine} system that takes no arguments and returns the context object.

This will enable to code listed above in the \textit{Rationale} to become:
\begingroup\small\begin{verbatim}
    # Get the name of the current class or class delegate 
    set cls [uplevel 1 self]
\end{verbatim}
\endgroup

In the C API, I propose adding a function:

\begin{quote}
Tcl\_Object \textbf{Tcl\_GetDefineContextObject}(Tcl\_Interp *\textit{interp})
\end{quote}

which will get the context object, or return NULL and put an error in the interpreter if there is no context object in the frame or the context object has been deleted. The functionality is that of \textbf{TclOOGetDefineCmdContext} in \textit{tclOODefineCmds.c} [\url{http://core.tcl-lang.org/tcl/artifact/7d58f1a701168168?ln=682}], but the text of the error messages might be changed.

\section{Copyright}

This document has been placed in the public domain.

\section{Colophon}

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