Version 8 of proc alias

Updated 2013-10-30 16:02:20 by pooryorick

How to make an alias for a proc

See Also

interp alias
proc
ycl
includes this alias in its proc module

Description

The common idiom for making an alias for a procedure is interp alias, but the byte-compiled versions of commands (not user procedures, but built-in commands) will not be used in that case. Here is an implementation of an idea suggested by MS for making aliases without, missing out on that byte-compiled command goodness:

proc alias {alias target} {
        set fulltarget [uplevel [list namespace which $target]]
        if {$fulltarget eq {}} {
                return -code error "no such command: $target"
        }
        namespace eval [namespace qualifiers $fulltarget] \
        [list namespace export [namespace tail $fulltarget]]
        set newcmd [namespace eval [info cmdcount] [string map [
                list @{alias} [list $alias] @{fulltarget} [list $fulltarget] ] {
                namespace import @{fulltarget}
                rename [namespace tail @{fulltarget}]  @{alias}
                ::namespace export @{alias}
                ::namespace which @{alias}
        }]]
        uplevel [list namespace import $newcmd]
        uplevel [list trace add command $fulltarget delete [list apply [list {stash args} {
                namespace delete [namespace qualifiers $stash]
        }] $newcmd]]
}

Example:

alias pset set
pset answer 42
puts $answer
rename ::pset {}