Ulysses
Ulysses

Reputation: 6015

Is there any way to rename 'set'

While trying to rename the set function, I got the error too many nested evaluations (infinite loop?) (see below).

Perhaps because rename uses set internally. Is there any way to override this behaviour?

rename set Set
too many nested evaluations (infinite loop?)
    while executing
"set cmd [lindex $args 0]"
    (procedure "::unknown" line 8)
    invoked from within
"set cmd [lindex $args 0]"
    (procedure "::unknown" line 8)
    invoked from within
"set cmd [lindex $args 0]"
    procedure "::unknown" line 8
    ...

Upvotes: 0

Views: 321

Answers (1)

Donal Fellows
Donal Fellows

Reputation: 137627

The rename command is a built-in command of Tcl, and does not depend on any other commands. However, the interactive evaluation loop does use set quite a bit in its history management code, as does the unknown command which handles what to do when a command name is not resolved to an existing command. That triggers an infinite loop as handling what to do when set is absent requires set to be present as part of the handling code; the stack protection code (see interp recursionlimit) eventually kills things off…

The simplest fix is to make sure that you keep something in place called set.

# Make our replacement
proc replacement_set {varName {value ""}} {
    # This is 8.6-specific code, but you can do other equivalent things
    tailcall old_set {*}[lrange [info level 0] 1 end]
}

# Swap things in, with both renames on the same line...
rename set old_set; rename replacement_set set

Note that you don't need to be quite as careful in a script.

Upvotes: 3

Related Questions