Version 1 of idiom

Updated 2001-06-29 12:37:44

[Introduce the notion of "idiom".]


[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


The whole area of event programming [L1 ].


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.


Arts and crafts of Tcl-Tk programming