idiom

An idiom is a sort of language hack in which the real meaning of a phrase is somewhat more obscure than its literal meaning. Idioms are often employed in the name of brevity or style. In programming languages, idioms often arise from emergent properties of the language that are discovered as it is used to create programs.

Each language, because of its distinct features inevitably affords unique idiomatic possibilities.

This page, of course, is primarily devoted to describing Tcl idioms.

Reference

Darmok

See Also

Meta Programming
presents several distinct but related idioms.
Tcl Programming Idioms , by Nat Pryce

Description

When this page is referenced, it is often in the sense:

an expression whose meaning is not predictable from the usual meanings of its constituent elements, as kick the bucket or hang one's head, or from the general grammatical rules of a language, as the table round for the round table, and that is not a constituent of a larger expression of like characteristics.

List of Idioms

@decorators
An abbreviated syntax for wrapping commands.
after idle [list after 0 ...]
A way to avoid having update and update idletasks get the Tcl event loop stuck in a cycle.
append somevarname {}
Mentioned elsewhere on this Wiki by AMG. Causes somevarname to be created if it doesn't exist, without changing its value if it does
lassign using foreach
If [lassign] is unavailable, instead of [lassign $vals {*}$vars], say [foreach $vars $vals {break}]. If it's guaranteed that [llength $list] <= [llength $vars], can say {} instead of {break}. The braces around break are optional and purely stylistic.
vwait forever
Enters the event loop and stays there until program termination because (presumably) the variable forever is never modified.
if 0 {...}
Comments out a block of text or code. Any mismatched braces in the commented-out code must be preceded by (an odd number of) backslashes.
proc Optional Arguments
cmdArgs(-linemap)
args or argv parsing facilitated through use of arrays such as cmdArs(-linemap). This is from the Pane Manager example, Practical programming in Tcl and Tk, by Brent Welch.
bind . <F2> {console show}
if {[catch {format %d%d 3 notanumber}]} ...
Validates multiple values against the specified conversion types.
unshared value idiom
Arrange for a value to be unshared so that Tcl doesn't copy it when it is modified. When passing a variable's value to a command, instead of saying $var, say $var[set var {}] so that its value is "taken" from the variable.
event-driven programming
lassign {} a b d
Assigns the empty string to multiple variables.
lindex somevalue
This is the shortest built-in identity function.
tags
When working with megawidgets, tags can be used to group components
static variables
bind . <Up> {exec wish $argv0 &; exit}
RS: Rapid restart after edits
The filter idiom
Tcl Idioms for Process Management

Self-Testing Source Fragments

See main script

New Control Structures and Error Reporting

Donal Fellows is good about reporting errors correctly. More precisely, Tcl programmers often knock together handy little control structures (repeat, do..while, ...) but frankly leave the error-traceback as a loose end for some indeterminate future clean-up. Donal's repeat example [L1 ] shows how to handle tracebacks cleanly. The heart of it is

global errorInfo
if {[uplevel #0 [list catch $cmd($rid) ::repeat::msg]]} {
    append errorInfo "\n    (\"repeat\" script)"
    bgerror $msg
    Stop $rid
    return
}

Performance Win with regsub

[Explain Miguel's generalization of 32 to bit-length with self-modifying code in Binary representation of numbers as one of several alternatives. regsub-ing a script can be a performance win.]

   [[And there's a class of idioms targeted at [Tcl performance], anyway.]]

[I (Miguel) think that Can you run this benchmark 10 times faster is also a good example ...]

Global Array of Data

Constants, globals, and so on: reference especially Bob Techentin's post: "One method that many people use is to declare a global array of related data, which you can then reference in your procs with a single global statement. Like this:

array set defaultData {
    color     red
    filetype  text
    cputime   100
}

proc myProc {} {
    global defaultData
    if {$defaultData(color) eq "green"} {
        puts {I can't tell the difference between red and green.}
    }
}

You can wrap this behavior into a proc, so you don't have to remember to declare the array global all the time:

proc const {key} {set ::defaultData($key)}
...
if {[const color] eq "green"} {...} ;#RS

Retrieving Version Information

Although old-timers cheerfully manipulate [info tclversion], $::tcl_version, [info glob exp*] (for Expect), and so on, the modern idiom for retrieving version information is

foreach package {Tcl Expect ...} {
    puts "The version is [package provide $package]."
}

Widget Root Names

Several people [L2 ] have thought about the not-all-root-names-are-OK-for-your-widget-tree quirk.

Proc with Automatic Default Lookup

TV I'm not sure what idiom is defined like exactly, but I did a procedure called pro_args which allows you to name and assign value to some of many arguments of a procedure, by naming only the ones you want it will automatically lookup defaults for the others.

So you'd have a procedure lets say newarray:

% info args newarray
nx ny bn fs ib ipi jb jpi x y

of which certain arguments have defaults, and you want to make sure $x and $y get values of, for instance, 100 and 50 respectively. Of course we could then type something like newarray a b c d e ... 100 50, with each argument, also the ones for which we want the default values, filled in or listed. pro_args lets you form such a command by using:

% pro_args newarray {{x 100} {y 50}}
newarray 3 3 array {} {} {} {} {} 100 50

And when it looks ok, you use

 eval [pro_args newarray {{x 100} {y 50}}]

to execute the command, in this case a bwise command to generate an array of blocks.

Maybe eval could be part of the command to save typing. This construction is useful for commands with more than a few or complicated arguments and defaults for them.

lappend to Ensure Existence

In modern Tcl, this idiom was rendered inoperable by the fix to the bug and mentioned below. Instead use [append varname {}].

jcw - To make sure a variable exists (and set it to empty otherwise), you can use lappend:

lappend a
if {$a ne {ok}} { ... }

or even:

if {[lappend a] ne {ok}} { ... }

Instead of the usual if {![info exists a]} {set a {}}.

slebetman: surely that should be:

catch {lappend a}

unless you don't mind your application unexpectedly exiting or popping up error messages from time to time.

LV: I'm curious as to what kind of error you were envisioning? I tried missing and present variable, and array references, and lappend didn't raise any errors there. I even tried this:

set a "{"
llength $a
lappend a

and even though llength complains about a having unmatched open brace in the list, lappend didn't complain at all.

slebetman: Oh yes it does complain:

% set a {"}
% lappend a x
unmatched open quote in list
% # Even the example you gave above complains:
% set a "{"
% lappend a x
unmatched open brace in list
% info patchlevel
8.4.12

what version are you using?

slebetman: oops sorry, it appears that it indeed doesn't complain:

% lappend a
{

Can we rely on all future versions of lappend to accept malformed lists in its argument if it has only a single argument or should we treat this as an undocumented, unsupported behavior (bug)?

LV: I don't know - watch this bug report [L3 ] for a response from the maintainers...