Version 6 of info default

Updated 2005-07-03 16:43:07

info default procname arg varname

Procname must be the name of a Tcl procedure and arg must be the name of an argument to that procedure. If arg doesn't have a default value then the command returns 0. Otherwise it returns 1 and places the default value of arg into variable varname.

Zarutian 3. july 2005: why does this sub-procedure require an varname? It realy should return an list with two elements. The first one is the boolean (predicate: this arg of this proc has default value?). The second one is the default value (Empty if the boolean is "0" (false)).

MG July 3rd 2005 - You can get the effect yourself easily enough yourself with a wrapper proc:

 proc defaultList {proc arg} {
  set hasDef [info default $proc $arg valDef]
  return [list $hasDef $valDef]
 }

The only problem in that is that the scoping of $proc may differ, between where [defaultList] is called and where the [info default] inside it is run. An uplevel would sort that, but then the var would be created in the wrong scope, and we'd have to upvar or uplevel to gain access to it, so it could still obliterate a variable in use already. But, if you wanted it...

 proc defaultList2 {proc arg} {
  set hasDef [uplevel info default $proc $arg infoDefaultVariable]
  set valDef [uplevel set infoDefaultVariable]
  uplevel unset -nocomplain infoDefaultVariable
  return [list $hasDef $valDef]
 }

And to test:

 % proc test {a {b defB}} {puts "a -> $a, b -> $b"}
 % defaultList test a
 0 {}
 % defaultList2 test b
 1 defB

Zarutian 3. july 2005: It would be easier the other way around, wouldnt it ;)

Then if you wanted to set an variable with the default value of an arg of an proc:

 proc defaultArg {proc arg var} {
   set res [info default $proc $arg]
   upvar $var tmp
   set tmp [lindex $res 1]
   return [lindex $res 0]
 }

MG Indeed it would. That was really just to show that you could achieve what you wanted, even with info default working as it does now. Perhaps the built-in implementation could be altered to work how you want, if no variable argument is passed (which currently results in an error), but still work as it does now if the variable is given.


See also:


Category Command - Tcl syntax help