namespace ensemble create -map : Get the old name in my procedure when I call it

Viewed 70

below my namespace :

namespace eval ::my {

    namespace ensemble create -map {
        name            name
        newName         name
    }
}

and my procedure :

proc ::my::name {args} {
    puts [lindex [info level 0] 0]
}

I would like to get the name of my procedure, for this I use [lindex [info level 0] 0].
But when I call : my newName, It always returns ::my::name.
Is It possible to get ::my::newName instead ?

2 Answers

No, because you are looking at the resolved implementation name. If you want information about the way in which your implementation is called, pass it as an extra argument:

namespace eval ::my {
    proc name {realName args} {
        puts $realName
    }
    namespace ensemble create -map {
        name            {name name}
        newName         {name newName}
    }
}

This has the advantage of being useful even if an abbreviated subcommand name is used (e.g., my new).

Depending on your use case, you may be able to use an execution trace:

trace add execution my enter [list apply [list {cmd args} {
    if {[lindex $cmd 0] eq "my"} {
        puts [lindex $cmd 1]
    }
}]]
Related