TIP #429: A 'STRING' SUBCOMMAND FOR CONCATENATION =================================================== Version: $Revision: 1.6 $ Author: Andreas Leitgeb Alexandre Ferrieux State: Final Type: Project Tcl-Version: 8.6.2 Vote: Done Created: Sunday, 27 July 2014 URL: https://tip.tcl-lang.org429.html Post-History: ------------------------------------------------------------------------- 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* or *string 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.tk. COPYRIGHT =========== This document has been placed in the public domain. ------------------------------------------------------------------------- TIP AutoGenerator - written by Donal K. Fellows