TIP #429: A 'string' Subcommand for Concatenation


TIP:429
Title:A 'string' Subcommand for Concatenation
Version:$Revision: 1.6 $
Authors: Andreas Leitgeb <avl at logic dot at>
Alexandre Ferrieux <alexandre dot ferrieux at gmail dot com>
State:Final
Type:Project
Tcl-Version:8.6.2
Vote:Done
Created:Sunday, 27 July 2014
Keywords:Tcl, cat, scriptlet result

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:

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.


Powered by Tcl[Index] [History] [HTML Format] [Source Format] [LaTeX Format] [Text Format] [XML Format] [*roff Format (experimental)] [RTF Format (experimental)]

TIP AutoGenerator - written by Donal K. Fellows