TIP 429: A 'string' Subcommand for Concatenation

Bounty program for improvements to Tcl and certain Tcl packages.
Tcl 2017 Conference, Houston/TX, US, Oct 16-20
Send your abstracts to tclconference@googlegroups.com
by Aug 21.
Author:         Andreas Leitgeb <avl@logic.at>
Author:         Alexandre Ferrieux <alexandre.ferrieux@gmail.com>
State:          Final
Type:           Project
Vote:           Done
Created:        27-Jul-2014
Keywords:       Tcl,cat,scriptlet result
Tcl-Version:    8.6.2


This TIP describes a new (sub)command string cat to concatenate an arbitrary number of strings.


Tcl has string concatenation built-in. But that is lacking in two specific cases:


I propose a new subcommand 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 list of the separate arguments and use 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 join [list ...] "", where the contents of the intermediate list are either a single word or many words, or 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 a and b:

 string equals $a$b [string cat $a $b]

Rejected Alternatives

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

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

Reference implementation

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


This document has been placed in the public domain.