\documentclass[]{article}
\usepackage{amsmath,graphicx,supertabular,hyperref,tabularx,ifthen}
\title{TIP \#465: Change Rule 8 of the Dodekalogue to Cut Some Corner Cases}
\date{March 03, 2017}
\author{Andreas Leitgeb}
\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 \#465}&\textbf{Change Rule 8 of the Dodekalogue to Cut Some Corner Cases}\\\hline
Author&

Andreas Leitgeb \tipmail{avl}{logic.at}
\\
Created&Friday, \(\text{3}^{\text{rd}}\) March 2017\\
Type&Project\\
Tcl Version&\(8.7\)\\
State&Draft\\
Vote&Pending\\
Version&\tipversion$Revision: 1.2 $\\
Post History&\\
\hline\end{tabularx}\end{center}
\thispagestyle{empty}\pagestyle{empty}
\begin{tipabstract}

This TIP proposes to make \textbf{\$}-substitution more conforming to naive expectations and just rule out certain odd-ball uses that can safely be assumed to not appear in serious use, but only in crafted examples ``serving'' for confusion, or as accidentally legal interpretation of mistyped Tcl code.
\end{tipabstract}
\tableofcontents\setcounter{page}{0}\clearpage\pagestyle{plain}

\section{Rationale}

Back in the days where \textbf{\$}-substitution was added to Tcl, it was designed to be as syntactically simple as possible. Back then, Tcl was still an interpreted language, so optimising parse time was top priority. For this, it was designed that a sequence starting with \textbf{\$\{} would end at the \textbf{next close brace} no matter how many open braces or backslashes are passed by, and \textbf{\$arr(} would end at the \textbf{next close paren} no matter how many open parens are found before. This enables odd-ball corner cases that work in an interactive shell at top level, as tried by newbies:
\begingroup\small\begin{verbatim}
   set "{{{" 42; puts ${{{{}
\end{verbatim}
\endgroup

but fail within a braced block, unless a comment like:
\begingroup\small\begin{verbatim}
   # }}} }}}
\end{verbatim}
\endgroup

follows within the same block. These are just strange parts of Tcl, that nobody can seriously claim to use for good.

Another surprising part is with arrays, where parens are treated asymmetrically, in that any number of bare open parens, but also quote chars or braces may be part of the index in a \textbf{\$arr(...)} substitution, but first bare close paren terminates the token. Quote characters or braces have no significance, apart from that bare close braces might pre-maturely finish the enclosing braced block, which may only be evident to seasoned Tclers --- and not even always to them.

The final motivation for writing up this TIP came while discussing one part of \cite{tip282}: assignment to array elements.

An informal poll showed a clear preference towards bare \textbf{array(...)} naming on left hand side of proposed assignment, without sympathy for any need of explicit disambiguation by quoting or tagging.

Generally disallowing bare open parens, quotes and braces within array indices would mean that array indices on left hand side of an assignment could follow same rules as on right hand side, and parsing an array by these new rules would make sure, that where parsing as a function call and parsing as an array are both successful, then both parses would end up consuming the same portion of the expression body --- a prerequisite for making a sound decision about following assignment operator.

Without having array parses and function parses agreeing on close paren, then it is possible that parsing as an array will see a trailing assignment operator that would otherwise have been nested in a subexpression, or even part of a quoted literal value.

Because of the low expected impact on real code, a target of 8.7 is considered feasible.

\section{Implementation}

A full implementation of this TIP is now checked in on branch \textit{tip-465}.

\section{Alternatives}

The following points show alternatives that would make sense, but would make the currently rather simple implementation of this TIP ways more complicated:
\begin{itemize}
\item{}
Allow bare parens in array indices if properly paired. Quotes and braces are still disallowed (even if paired), to avoid cases of bad nesting: \textbf{\{(\})}. This might save users two backslashes in some rare cases where a close paren in the index is already backslash-escaped.
\item{}
Specifically add backslash-quoting to the ``body'' of \textbf{\$\{...\}}. After all, the ``body'' is a variable name, and not a nested structure like most braced words in Tcl. This would make some odd variable names once again possible, but now in a consistent syntax that doesn't affect enclosing blocks.
\item{}
\textbf{\$\{...\}} syntax could also be further restricted by disallowing open braces and final backslashes, just to enforce ``well-behaving'' tokens.
\item{}
A much stricter alternative would disallow unbalanced braces even within ``-quoted and unquoted literals. This would disallow common but dangerous idioms like \textit{append var ''\{``}, which may be followed by an \textit{append var ''\}"} in the same block and work, until one of these two commands gets moved into a nested block. The correct and safe way is, of course, backslash-escaping bare braces within string literals. Good Code(tm) wouldn't be affected by this alternative change.
\end{itemize}

\section{Copyright}

This document has been placed in the public domain.

\section{Colophon}

\textit{TIP AutoGenerator --- written by Donal K. Fellows}
\begin{thebibliography}{TIP \#1}
\addcontentsline{toc}{section}{References}
\bibitem[TIP \#282]{tip282}
Will Duquette,
Don Porter,
\emph{Enhanced Expression Syntax},
on-line at \url{http://purl.org/tcl/tip/282.html}
\end{thebibliography}
\end{document}
