Version 4 of multi-argument set

Updated 2005-12-11 22:55:30 by suchenwi

Richard Suchenwirth 2005-12-12 - In the Tcl chatroom, CMcC said:

 You know, in retrospect, I think [set x a b c] should be equivalent to [set x [list a b c]]

Well, Tcl is where wishes come true :) Here's my experimental code that overloads set, and seems to work fine, as far as I've tested. I had to reuse the _var name for upvar, so the error message comes right if the variable doesn't exist yet:

 rename set _set
 proc set {_var args} {
    upvar 1 $_var $_var
    _set ll [llength [info level 0]]
    if {$ll==3} {
        _set $_var [lindex $args 0]
    } elseif {$ll>3} {_set $_var $args}
    _set $_var
 }

The distinction between $ll==3 and $ll>3 is necessary so this frequent case comes right:

 set something {}

Testing:

 % source myset.tcl
 % set foo
 can't read "foo": no such variable
 % set foo 1
 1
 % set foo
 1
 % set bar 2 3 4
 2 3 4
 % set bar
 2 3 4
 % set grill {{}}
 {}
 % set grill
 {}
 % set nix {}
 % set nix
 %

So far it looks as expected. But bytecode compilation may lose performance if a fundamental like set is pulled from under its feet...