Version 17 of idiom

Updated 2006-10-04 07:45:41

[Introduce the notion of "idiom".]

Well, when I look up idiom [L1 ] I am uncertain in what vein the term is being used here.

Perhaps as "a style or manner of expression peculiar to a programming language " ?


[idiom for procs with optional leading arguments, followed by fixed later arguments]


Idiomatic use of foreach in place of lassign.


vwait forever


args or argv parsing facilitated through use of arrays such as cmdArs(-linemap). Is this from Brent's book? RS: Yes, at the beginning of the Pane Manager, 2nd ed.p279


bind . <F2> {console show}


The whole area of event programming: Event programming and why it is relevant to Tcl/Tk programming


When you start to think about megawidgets and/or widget subclassing, consider whether creative use of tags is a good Tk solution. In many cases, it's enough to "gang" different elements together with tags.


Meta Programming collects together several distinct, if related, idioms.


static variables


An idiom useful for self-testing source fragments and documentation uses is this one, as formulated in a clt posting by DKF:

    # Test for running stand-alone *or* in the plugin.  Follow an idea
    # through to its logical conclusion...
      if {[string equal $::argv0 [info script]] || \
          [array exists ::embed_args]} {
        main
      } 











Also see Nat Pryce's "Tcl Programming Idioms" [L2 ].


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 [L3 ] 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
        }










[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 ...]


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) == "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] == "green"} {...} ;#RS


    if 0 {
    Stuff
    }

is an alternative comment mechanism.


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]."
    }

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


 bind . <Up> {exec wish $argv0 &; exit} ;# RS - rapid restart after edits

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 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.


The filter idiom


Tcl Idioms for Process Management


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?


[ Arts and crafts of Tcl-Tk programming - Category Glossary ]