Thanks for the reply jm,
In the simplest case nothing would change. proc continues to work with positional arguments the same way.
What both tips address are cases where we need to be flexible about the order in which arguments arrive:
8<-----------------------------------
@proc exampleproc {
val1 {}
val2 {}
val3 {}
} {
puts "$val $val2 $val3"
}
exampleproc val1 1 val2 2 val3 3
----------------------------------->8
In tip#479. The empty set next to each argument is actually a dictionary that allows the developer to
ascribe behavior to the arguments. Set a default value if not given, or state whether a variable is
mandatory or not.
The new behavior complicates simple procs, but if you have a procedure that takes in a number of
parameters, and if that proc has a habit of changing and/or needs to accept incomplete input,
having named procedures can be essential.
The tip also allows you to mimic Tk's metaphor:
8<-----------------------------------
proc my_psuedowidget {path {args {
color {default blue aliases {-color -bg}}
flavor {default vanilla aliases -flavor}
borderwidth [default 0 aliases {-bd -borderwidth}}
}} {
label $path -text $flavor -color $color -bd $borderwidth
}
my_psuedowidget .label -flavor mango -bg purple -bd 10
----------------------------------->8
The tip also provides and arg eater called @args which can be nice for inside of ensembles.
8<-----------------------------------
proc dispatch {command args} {
switch $command {
smtp {
# Dicts can be created on the fly
@args [dict create from {} to {} mtime [dict create default [clock seconds]]] $args
smtp::dispatch $from $do mtime $mtime
}
http {
# Or accept a static string with whitespace
@args {
from {}
to {}
subject {}
url {default
http://mydomain.info}
} $args
http::post ${url}?from=${from}&to=${to}&subject=${subject}
}
}
}
----------------------------------->8
Since the tip also builds on that args mechanic, you can also do dict processing for optional (or unexpected) parameters.
8<-----------------------------------
@proc pedantic {
from {}
to {}
subject {}
} {
set fields [dict keys ${@spec}]
foreach key [dict keys $args] {
if {$key ni $fields} {
error "Unknown parameter $key. Valid: $fields"
}
}
}
8<-----------------------------------
In the above case, if the user feeds in a field other than to, from or subject the proc will throw an error.