\documentclass[]{article}
\usepackage{amsmath,graphicx,supertabular,hyperref,tabularx,ifthen}
\title{TIP \#371: Improvements for the dict command}
\date{August 05, 2010}
\author{Thomas Perschak, Trevor Davel}
\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 \#371}&\textbf{Improvements for the dict command}\\\hline
Author&

Thomas Perschak \tipmail{tombert}{gmx.at}
\par
Trevor Davel \tipmail{twylite}{crypt.co.za}
\\
Created&Thursday, \(\text{5}^{\text{th}}\) August 2010\\
Type&Project\\
Tcl Version&\(8.7\)\\
State&Draft\\
Vote&Pending\\
Version&\tipversion$Revision: 1.3 $\\
Post History&\\
\hline\end{tabularx}\end{center}
\thispagestyle{empty}\pagestyle{empty}
\begin{tipabstract}

The \textbf{dict} command is limited by allowing manipulation of only the first level of key elements. Not only should the \textbf{dict get} accept a nested key list, but also the other commands like \textbf{dict replace}.
\end{tipabstract}
\tableofcontents\setcounter{page}{0}\clearpage\pagestyle{plain}

\section{Specification And Example}

The following line:
\begingroup\small\begin{verbatim}
dict get {-range {-values {a b c} -base M} -name myname} \
        -range -base
\end{verbatim}
\endgroup

Results in:
\begingroup\small\begin{verbatim}
M
\end{verbatim}
\endgroup

But how to change the base? I suggest the following \textbf{dict} syntax extension:
\begingroup\small\begin{verbatim}
dict replace {-range {-values {a b c} -base M} -name myname} \
        {-range -base} k
\end{verbatim}
\endgroup

Results in:
\begingroup\small\begin{verbatim}
-range {-values {a b c} -base k} -name myname
\end{verbatim}
\endgroup

Allowing a nested key list would not break any previous code, but substantially improve the \textbf{dict} command.

\section{Rationale}

The \textbf{dict} command is the basis for handling database like structures. By allowing nested keys this would give more freedom in organizing these structures like the example above.

\vspace{3ex}\hrule\vspace{1.5ex}

\section{Comments}

Twylite 2010/08/17: The specification states that ``Allowing a nested key list would not break any previous code''. This is not correct, for example:
\begingroup\small\begin{verbatim}
dict replace {"Jane Smith" "11 Foo Road" "John Doe" "Address unknown"} {Jane Smith} new_address
-> {Jane Smith} new_address {John Doe} {Address unknown}
\end{verbatim}
\endgroup

Existing code that uses \textbf{dict replace} in conjunction with keys that are valid lists of 2 or more elements would break.

Also, drawing from experience, the nested key approach is a source of subtle bugs. It is natural to write code such as:
\begingroup\small\begin{verbatim}
dict replace $dict $key $value
\end{verbatim}
\endgroup

but that contains a bug that is often missed during testing. The correct approach would be:
\begingroup\small\begin{verbatim}
dict replace $dict [list $key] $value
\end{verbatim}
\endgroup

\vspace{3ex}\hrule\vspace{1.5ex}

\section{Copyright}

This document has been placed in the public domain.

\section{Colophon}

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