From: iand on
Hi All,

Our work involves modularizing the existing code a bit too much. So we
have to write many procedures (procs) which recognizes keyword
arguments and call them later. We know, we can implement this feature
into tcl procs using switch statement the way we are using now. I
agree also many packages are available for the same. But all this adds
a bit more unnecessary lines to the code with all the exception and
error handling. Imagine if we have to write hundred such procs!!!
Would'nt it be helpful if TCL procs had a keyword-argument recognition
mechanism? Or am i asking too much?
"args" is already helpful. I already like tcl procs and its features
which already helps us in implementing many tasks (like assigning def
values to args). It seems to lack only the one i discussed or it
exists already?

Like:
proc p {arg1 argk {keywordarg1 defval} {keywordarg2 defval} } {
body
}

Calling proc p:
p keywordarg1 val1 keywordarg2 val2

Like the way "args" distinguishes itself from other arguments there
should be something to distinguish keyword arguments in tcl procs. I
have used "argk" to denote what follows are keyword arguments.

Regards,
iand.












From: Arndt Roger Schneider on
iand schrieb:

>Hi All,
>
>Our work involves modularizing the existing code a bit too much. So we
>have to write many procedures (procs) which recognizes keyword
>arguments and call them later. We know, we can implement this feature
>into tcl procs using switch statement the way we are using now. I
>agree also many packages are available for the same. But all this adds
>a bit more unnecessary lines to the code with all the exception and
>error handling. Imagine if we have to write hundred such procs!!!
>Would'nt it be helpful if TCL procs had a keyword-argument recognition
>mechanism? Or am i asking too much?
>"args" is already helpful. I already like tcl procs and its features
>which already helps us in implementing many tasks (like assigning def
>values to args). It seems to lack only the one i discussed or it
>exists already?
>
>Like:
>proc p {arg1 argk {keywordarg1 defval} {keywordarg2 defval} } {
>body
>}
>
>Calling proc p:
>p keywordarg1 val1 keywordarg2 val2
>
>Like the way "args" distinguishes itself from other arguments there
>should be something to distinguish keyword arguments in tcl procs. I
>have used "argk" to denote what follows are keyword arguments.
>
>Regards,
>iand.
>
>
>
>
>
/The Tcl-language does not support named
arguments./

Tcl 8.5 introduced the *namespace ensemble*:

http://tmml.sourceforge.net/doc/tcl/namespace.html
original implemented in itcl as:
http://www.eso.org/projects/vlt/sw-dev/tcl8.3.3/itcl3.2.1/ensemble.n.html

Also see inside the wiki (outdated):
http://wiki.tcl.tk/4302

-roger
From: Ronnie Brunner on
On 2010-05-26 08:49, iand wrote:
> Our work involves modularizing the existing code a bit too much. So we
> have to write many procedures (procs) which recognizes keyword
> arguments and call them later. We know, we can implement this feature
> into tcl procs using switch statement the way we are using now. I
> agree also many packages are available for the same. But all this adds
> a bit more unnecessary lines to the code with all the exception and
> error handling. Imagine if we have to write hundred such procs!!!
> Would'nt it be helpful if TCL procs had a keyword-argument recognition
> mechanism? Or am i asking too much?

Yes. Just write your own 10 line (OK, 20 line) mechanism that does that.
(See below)

> "args" is already helpful. I already like tcl procs and its features
> which already helps us in implementing many tasks (like assigning def
> values to args). It seems to lack only the one i discussed or it
> exists already?
>
> Like:
> proc p {arg1 argk {keywordarg1 defval} {keywordarg2 defval} } {
> body
> }
>
> Calling proc p:
> p keywordarg1 val1 keywordarg2 val2

You might want to just wrap proc somehow like this:

proc namedargproc {name keys body} {
foreach key $keys {
if {[llength $key] > 1} {
set optional([lindex $key 0]) [lindex $key 1]
} else {
lappend required($key) 1
}
}
set parsing "[list array set arguments [array get optional]]\n"
append parsing "[list array set required [array get required]]\n"
append parsing {
foreach {k v} $args {
set arguments($k) $v
if {[info exists required($k)]} {
unset required($k)
}
}
if {[llength [array names required]]} {
error "missing key(s): [array names required]"
}
foreach {k v} [array get arguments] {
set $k $v
}
}
proc $name {args} "$parsing\n$body"
}

(note: there are some limitations regarding variable names in the sample
that could be prevented with some namespace magic or so...)

With such a wrapper, you can simply define procs that use named
parameters only:

namedargproc foo {a b {c 3} {d 4}} {
puts "a: $a, b: $b, c: $c, d: $d"
}

% foo a 1
missing key(s): b
% foo a 1 b 2
a: 1, b: 2, c: 3, d: 4
% foo b 1 c 7
missing key(s): a
% foo b 1 c 6 a 2
a: 2, b: 1, c: 6, d: 4
%

hth
Ronnie