\documentclass[]{article}
\usepackage{amsmath,graphicx,supertabular,hyperref,tabularx,ifthen}
\title{TIP \#429: A 'string' Subcommand for Concatenation}
\date{July 27, 2014}
\author{Andreas Leitgeb, Alexandre Ferrieux}
\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 \#429}&\textbf{A 'string' Subcommand for Concatenation}\\\hline
Author&

Andreas Leitgeb \tipmail{avl}{logic.at}
\par
Alexandre Ferrieux \tipmail{alexandre.ferrieux}{gmail.com}
\\
Created&Sunday, \(\text{27}^{\text{th}}\) July 2014\\
Type&Project\\
Tcl Version&\(8.6.2\)\\
State&Final\\
Vote&Done\\
Version&\tipversion$Revision: 1.6 $\\
Keywords&Tcl, cat, scriptlet result\\
Post History&\\
\hline\end{tabularx}\end{center}
\thispagestyle{empty}\pagestyle{empty}
\begin{tipabstract}

This TIP describes a new (sub)command \textbf{string cat} to concatenate an arbitrary number of strings.
\end{tipabstract}
\tableofcontents\setcounter{page}{0}\clearpage\pagestyle{plain}

\section{Rationale}

Tcl has string concatenation built-in. But that is lacking in two specific cases:
\begin{itemize}
\item{}
one cannot directly concat a braced string with anything else
\item{}
scriptlets such as used for \textbf{lmap} are expected to contain commands, the last one of which returns a value. To have the scriptlet return a concatenated string or even just a single string literal, one currently needs to misuse some corner-case of a non-trivial command, like \textbf{return -level 0 \$x\$y} or \textbf{string map \{\} ``\$x\$y''} just to have the scriptlet produce the string as its result.
\end{itemize}

\section{Proposal}

I propose a new subcommand \textbf{string cat}, that will take an arbitrary number of arguments (i.e., 0 or more), and concatenate them into a single string that becomes the result of the command.

It would be equivalent to creating a \textbf{list} of the separate arguments and use \textbf{join} on that list with an empty string as second argument.

Compiling that new command to bytecode should be trivial, as concatenation of strings is already compileable. The added value would be allowing braced string literals to be involved, and promoting the resulting stack-item to the result of the command/scriptlet. (This simple compileability is also meant to be a main advantage over \textbf{join [list ...] ""}, where the contents of the intermediate list are either a single word or many words, or \textbf{lindex [list ...] 0} where the contents of the intermediate list are a single word.)

The following equality will hold for any arbitrary contents of the variables \textbf{a} and \textbf{b}:
\begingroup\small\begin{verbatim}
 string equals $a$b [string cat $a $b]
\end{verbatim}
\endgroup

\section{Rejected Alternatives}

Lars has mailed on tclcore that TclX has a command \textbf{cconcat} that does essentially what my proposed \textbf{string cat} is supposed to do (not sure though whether that is compiled). This proposal sticks to the \textbf{cat} subcommand, as that is generally the preferred way over new toplevel commands.

Also, \textbf{string concat} is added to this section, for it is a bit longer than \textbf{string cat}, and (as Lars put it) \textbf{string cat} is less likely to be misinterpreted as ``concat, just moved into the string ensemble.''

\section{Reference implementation}

Available as branch tip-429 on core.tcl.tk.

\section{Copyright}

This document has been placed in the public domain.

\section{Colophon}

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