How to declare optional arguments in proc - tcl

Could you help me how to use arguments with optionals I used this function but it does not work
proc {my_proc} {n1 n2 -args{{u2 "5"} {u1 "5"}} } {
puts "n1:'$n1', n2:'$n2', u1:'$u1', u2:'$u2'"
}
->my_proc 1 -args 5 7
n1:'1', n2:'$n2', u1:'7', u2:'5'
I would like to call function like
my_proc 1 -args {u2 5} {u1 7}
my_proc 1 {u2 5} {u1 7} (required + optional arguments)
my_proc 1 (only required arguments)

You are strongly recommended to use only one of these patterns in a particular command:
Optional arguments, where the optional-ness is by position.
Optional key-value pairs.
Combining the two is relatively hard to get right, and is always rather confusing!
Optional by position
proc my_proc {n1 n2 {u2 "5"} {u1 "5"}} {
puts "n1:'$n1', n2:'$n2', u1:'$u1', u2:'$u2'"
}
my_proc 7 8 9
#### n1:'7', n2:'8', u1:'5', u2:'9'
Optionality by key-value pair
proc my_proc {n1 n2 args} {
# Add the defaults
set args [dict merge {-u1 5 -u2 5} $args]
# Magic! (Keys start with “-” by arbitrary convention.)
# Copies from the value for key “-u1” to $u1 (and similarly “-u2”/$u2)
# The empty value is an update script; empty here as we don't want to update
dict update args -u1 u1 -u2 u2 {}
# Use...
puts "n1:'$n1', n2:'$n2', u1:'$u1', u2:'$u2'"
}
my_proc 7 8 -u1 123 -u2 456
#### n1:'7', n2:'8', u1:'123', u2:'456'
There's a few other ways to do this, e.g., with dict set options $args;puts $options(-u1). These are particularly useful in Tcl 8.4 (and before, for the truly behind-the-times):
proc my_proc {n1 n2 args} {
# Defaults
array set opt {-u1 5 -u2 5}
# Parse
array set opt $args
# Use
puts "n1:'$n1', n2:'$n2', u1:'$opt(-u1)', u2:'$opt(-u2)'"
}
my_proc 7 8 -u1 123 -u2 456
#### n1:'7', n2:'8', u1:'123', u2:'456'

As Donal suggested, I like to use args and an array to handle options. It allows an easy way to set default values:
proc p {args} {
array set options {-u1 defU1 -u2 defU2} ;# the default values
array set options $args ;# merge the user's values
parray options
}
p -foo bar -u1 42
options(-foo) = bar
options(-u1) = 42
options(-u2) = defU2
You will need to check that $args contains an even number of elements:
% p 1 2 3
list must have an even number of elements

Related

TCL : array is a collection of variables

I am trying to understand the difference between TCL arrays and TCL dictionary
There I get a statement :- "TCL array is a collection of variables while TCL dictionary is a collection of values"
Can somebody explain what it actually means. Programming example to understand the same will be great
Thanks
With an array, these are actually three separate variables.
set my_array(a) 1
set my_array(b) 2
set my_array(c) 3
Each variable exists separately:
info exists my_array(a) --> 1
info exists my_array(b) --> 2
info exists my_array(c) --> 3
puts "$my_array(a) $my_array(b) $my_array(c)" --> 1 2 3
The name my_array is also recognized to exist, but is actually a special name for the collection of the three individual variables. It's not really a regular variable that has its own value.
info exists my_array --> 1
puts $my_array --> can't read "my_array": variable is array
With a dictionary, there is one variable. The value of the variable represent the key/value pairs.
set my_dict "a 1 b 2 c 3"
info exists my_dict --> 1
info exists my_dict a --> wrong # args: should be "info exists varName"
puts $my_dict --> {a 1 b 2 c 3}
dict keys $my_dict --> {a b c}
dict get $my_dict a --> 1
dict values $my_dict --> {1 2 3}
In this example, the value of my_dict is {a 1 b 2 c 3}. You can pass $my_dict as an argument to a proc and the value is used by the proc. You can also return a dict from a proc. You cannot do that with an array in the same way.
One of the big differences is that you can trace add variable on an element of an array, whereas you can pass a dictionary into a procedure and return it from a procedure without needing to fiddle around with upvar or array get/array set.
proc didSetOnVarElement {name1 name2 op} {
puts "did $op on ${name1}($name2)"
}
trace add variable x(a) write didSetOnVarElement
set x(a) 1
set x(b) 2
incr x(a)
# Try comparing the output of that with what you get when you set a trace on the whole of x
Tracing array elements can be really useful, especially in Tk. So can passing things back and forth cheaply. You can't have them together though: values don't have identity in Tcl, whereas variables do (and it is exactly that identity — that name — that makes them modifiable and traceable entities while preventing them from being passed around trivially).
In addition to Chris's excellent answer:
A dictionary can be passed to a proc as a value
proc printItByValue {value} {
puts $value
}
set myDict [dict create a 1 b 2 c 3] ;# just another way to create a dict
printItByValue $myDict ;# => a 1 b 2 c 3
An array must be passed to a proc differently:
array set myArray {a 1 b 2 c 3} ;# just another way to populate an array
printItByValue $myArray ;# error as shown by Chris
printItByValue myArray ;# only prints the string "myArray"
We could extract the arrays contents and pass that to a proc as a list:
proc printArrayByValue {arrayContents} {
array set localArray $arrayContents
parray localArray
}
printArrayByValue [array get myArray]
localArray(a) = 1
localArray(b) = 2
localArray(c) = 3
parray is a handy utility proc to pretty-print an array.
Or, we can use the upvar command to link the passed variable name to the caller's stackframe.
proc printArrayByName {varName} {
upvar 1 $varName localArray
parray localArray
}
printArrayByName myArray ;# same output as above
Note that these procs print out the array using the local name. To get the array to print with the same name, we can do this tricky thing to link the local variable to the caller's variable with the same name:
proc printArrayByName {varName} {
upvar 1 $varName $varName
parray $varName
}
printArrayByName myArray ;# same output as above
myArray(a) = 1
myArray(b) = 2
myArray(c) = 3

How to read the variable Name which store the value

TCL Program Sample:
proc fun { x } {
puts "$$x = $x"
}
set a 10
fun $a
In this above program which prints the output as $10 = 10 But i would like to get a = 10 has the output. The variable which passes the values has to be read and the corresponding values as well. Is there a way to read the variable name.
proc fun name {
upvar 1 $name var
puts "$name = $var"
}
set a 10
fun a
The upvar command takes a name and creates an alias of a variable with that name.
Documentation:
proc,
puts,
set,
upvar
If you've got a currently-supported version of Tcl (8.5 or 8.6), you can use info frame -1 to find out some information about the caller. That has all sorts of information in it, but we can do a reasonable approximation like this:
proc fun { x } {
set call [dict get [info frame -1] cmd]
puts "[lindex $call 1] = $x"
}
set a 10
fun $a
# ==> $a = 10
fun $a$a
# ==> $a$a = 1010
Now, the use of lindex there is strictly wrong; it's Tcl code, not a list (and you'll see the difference if you use a complex command substitution). But if you're only ever using a fairly simple word, it works well enough.
% set x a
a
% set a 10
10
% eval puts $x=$$x
a=10
% puts "$x = [subst $$x]"
a = 10
% puts "$x = [set $x]"
a = 10
%
If you are passing the variable to a procedure, then you should rely on upvar.

Set 2 default variables in a tcl procedure

When defining a procedure in tcl like the one below, How can I call the proc defining just a and c? Is there any way to do this?
proc test1 { a {b 2} {c 3} } {
puts "$a $b $c"
}
Here's one technique, a bit messier that what you're hoping for but not too messy:
proc test1 { args } {
# set the default values
array set values {b 2 c 3}
# todo: validate that $args is a list with an even number of items
# now merge in the args
array set values $args
# and do stuff with the values ...
parray values
}
test1 a 10 c 14
You sometimes see applications use this technique where the array keys have a leading dash, to look like options:
proc test1 args {
array set values {-b 2 -c 3}
array set values $args
parray values
}
test1 -a 10 -c 14
Thanks Glenn and Peter, I joined your posts and I got
proc test1 { a args } {
array set valores [list a $a -b 2 -c 3]
array set valores $args
puts "$valores(a) $valores(-b) $valores(-c)"
}
which solved what I wanted.
So now I can call
> proc 12 -c 8
> 12 2 8

TCL proc for default value assignment

What is the syntax for a proc in tcl which automatically takes arguments if user din't give any arg?
I used something like
proc a {{args 1}} {
puts $args
}
a
when I used this I dint get my args value as 1. It returns a blank string. Please help me with this.
It sounds like you're trying to do two different things together:
Accept a variable number of arguments to your proc
If no arguments are provided, use default value(s)
There isn't really a syntax for proc that handles this case specifically. However, it's fairly easy to accomplish. You can accept a variable number of arguments (args keyword), and then check to see if any were supplied (and use a default value if not).
proc myproc {args} {
if { [llength [info level 0]] < 2 } { #called with no args
set args {the default list of values}
}
# rest of the code goes here
}
The info level 0 command returns the actual command being run as it was called, with arguments. Hence, if the length of it's result is < 2, we know the current command was called with no arguments (since the first element in the list is the actual command name itself).
The word args is a reserved word that has a specific meaning in proc definitions. Use a different variable name:
proc a {{x 1}} {
puts $x
}
a
Additional answer
In a proc definition, default values are given by defining the argument as a list with two members: the first will be the name of the argument, the second is the default value.
The word args is a special argument that implements rest arguments (that is, it captures remaining arguments not specified in the argument list). This is how one can implement variadic functions in Tcl.
The args arguments and default values can be used together when defining procs. But args cannot have default values. Any argument listed before args can have default values. But arguments that have default values must be listed after arguments without default values. So, basically you can write a function like this:
proc a {x {y 1} args} {
puts "$x $y ($args)"
}
a 1 ;# prints 1 1 ()
a 1 2 ;# prints 1 2 ()
a 1 2 3 4 ;# prints 1 2 (3 4)
If your use-case meets this pattern then you can define arguments with default values before args like the example above. If not, then your only option is to process args yourself:
# Assume parameters are key-value pairs,
# if value not given then default to 1
proc b args {
foreach {x y} $args {
if {$y == ""} { set y 1 }
puts "$x -> $y"
}
}
b 1 2 ;# prints 1 -> 2
b 1 2 3 ;# prints 1 -> 2, 3 -> 1

how to pass variable arguments from one function to other in tcl

I want to pass variable arguments obtained in one function to other function but I am not able to do so. Function gets even number of variable arguments and then it has to be converted in array. Below is the example.
Procedure abc1 gets two arguments (k k) and not form abc1 procedure these have to be passed to proc abc where list to array conversion it to be done. List to array conversion works in proc1 i.e. abc1 but not in second proc i.e. abc
Error obtained is given below
proc abc {args} {
puts "$args"
array set arg $args
}
proc abc1 {args} {
puts "$args"
array set arg $args
set l2 [array get arg]
abc $l2
}
abc1 k k
abc k k
Output:
k k
{k k}
list must have an even number of elements
while executing
"array set arg $l1"
(procedure "abc" line 8)
invoked from within
"abc $l2"
(procedure "abc1" line 5)
invoked from within
"abc1 k k"
(file "vfunction.tcl" line 18)
Best Solution: Expansion Substitution
The right approach is to ensure that the outer procedure (in stack terms) calls the inner one correctly; if multiple arguments are expected, that's what should be supplied. With the advent of Tcl 8.5, that's trivially done with a little language syntax called an expansion substitution:
proc abc1 {args} {
puts "$args"
array set arg $args
set l2 [array get arg]
abc {*}$l2
# Or combine the two lines above into: abc {*}[array get arg]
}
All the {*} does is say that the rest of the word should be broken up (using list syntax rules) and used as multiple arguments instead of Tcl's default “one visual word forms a single word” rules. It's ideal for this.
Old Solution: Eval Command
If you're still using old versions of Tcl for some reason (i.e., Tcl 8.4 or before) then you use the eval command instead of the above syntax:
eval abc $l2
There are some somewhat-more-efficient approaches to the above eval, which you might see in older code; for example:
eval [linsert $l2 0 abc]
eval [list abc] [lrange $l2 0 end]
# ... etc ...
But really they're all rendered obsolete by abc {*}$l2 which is shorter, simpler to write, and faster. (It's just not available in 8.4 or before, and too many deployments have yet to upgrade.) Use the expansion syntax if you can. Indeed, idiomatic Tcl code for 8.5 and later hardly ever needs eval; the extent to which this has proved true has even been rather surprising to the language's maintainers.
There's a big difference between
abc k k
and
abc [array get arg]
In the first case, you're passing two arguments, each of which is k. In the second case you're passing a list of things - in your example, a list of two k's: k k.
Nir's answer knowingly hacks around this problem, but the better solution is to write abc1 so that it calls abc properly:
proc abc1 {args} {
array set arg $args
set l2 [array get arg]
eval abc $l2
# or just
# eval abc $args
}
When you pass abc $l2 you are passing abc1's args as a single argument to abc. So in abc args holds a list with a single item ({k k}).
You can do something like this:
proc abc {args} {
#the next line assumes that if $args has only a single item, then it is
#a list in itself. It's a risky decision to make but should work here.
if { [llength $args] == 1 } { set args [lindex $args 0] }
puts "$args"
array set arg $args
}