Ray Salemi
Ray Salemi

Reputation: 5903

Call TCL proc with named arguments

Given this proc:

proc foo {{aa "a"} {bb "b"} cc} {
  echo $cc
}

Is it possible to call proc foo and only pass a value for cc? Also, is it possible to pass a value to cc explicitly by name?

Everything I see suggests that all arguments must be passed by position.

Upvotes: 5

Views: 5207

Answers (3)

Ron Fox
Ron Fox

Reputation: 11

You can use type shimmering to avoid constructing the dict..and this gives an interestingly simple and intuitive call. Pass the list representation of the dict to the proc e.g.:

  proc foo {a} {
      if {[dict exists $a cc]} {
          puts [dict get $a cc]
      }
  }

  foo {cc "this is the cc argument value" dd "this is the value of dd}

Upvotes: 1

glenn jackman
glenn jackman

Reputation: 246744

I would do something like Tk does:

proc foo {cc args} {
    # following will error if $args is an odd-length list
    array set optional [list -aa "default a" -bb "default b" {*}$args]
    set aa $optional(-aa)
    set bb $optional(-bb)

    puts "aa: $aa"
    puts "bb: $bb"
    puts "cc: $cc"
}

then

% foo
wrong # args: should be "foo cc ..."
% foo bar
aa: default a
bb: default b
cc: bar
% foo bar -bb hello -aa world
aa: world
bb: hello
cc: bar

Upvotes: 8

andy mango
andy mango

Reputation: 1551

Arguments to procedures are passed by position. The use of two element lists as a component of the argument list, e.g. {aa a}, is to supply a default value. However as the manual says:

Arguments with default values that are followed by non-defaulted arguments become required arguments. In 8.6 this will be considered an error.

So, most people design their procedure interfaces to place the arguments that supply a default value at the end of the argument list.

One way to simulate named arguments is to design the procedure to take a single argument that is a dictionary value where the keys of the dictionary are the names of the parameters. For example,

proc foo {a} {
    if {[dict exists $a cc]} {
        puts [dict get $a cc]
    }
}

foo [dict create cc "this is the cc argument value"]

If you don't mind the tedium of determining which optional parameters are supplied and the tedium of building a dictionary to invoke the procedure, then you can simulate named arguments this way and achieve some flexibility in supplying arguments to procedures. I don't use this technique very often myself. I find it easier to design the procedure interfaces more carefully. But sometimes the circumstances warrant all sorts of things.

Upvotes: 0

Related Questions