Author: Andreas Leitgeb <[email protected]>
Author: Alexandre Ferrieux <[email protected]>
State: Final
Type: Project
Vote: Done
Created: 27-Jul-2014
Post-History:
Keywords: Tcl,cat,scriptlet result
Tcl-Version: 8.6.2
Tcl-Branch: tip-429
Abstract
This TIP describes a new (sub)command string cat to concatenate an arbitrary number of strings.
Rationale
Tcl has string concatenation built-in. But that is lacking in two specific cases:
one cannot directly concat a braced string with anything else
scriptlets such as used for 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
return -level 0 $x$y
orstring map {} "$x$y"
just to have the scriptlet produce the string as its result.
Proposal
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-lang.org.
Copyright
This document has been placed in the public domain.