Version 4 of named parameters

Updated 2008-09-09 13:04:43 by SL

dict makes named parameters easy.

 proc example {a b args} {
   if {[llength $args] == 1} {
     set args [lindex $args 0] ;# work with unexpanded args
   }

   # establish defaults
   set args [dict merge {default value} $args]

   # use elements from dict
   fn [dict get $args parameter]


   # (dangerously) use args dict as variables
   dict with args {
      lappend parameter "END"
   }
 }

Alternatively, one can use an array to the same effect:

 proc example {a b args} {
   if {[llength $args] == 1} {
     set args [lindex $args 0] ;# work with unexpanded args
   }

   # establish defaults
   array set A {default value}
   # pour in supplied args
   array set A $args

   # use elements from A
   fn $A(parameter)

   # array elements are variables, so there is no need to unpack them explicitly.
   lappend A(parameter) "END"
 }

slebetman: Also see optproc for another take on named parameters.


SL 9/9/08: This needs some more explanation.