import proc from namespace with other name
In Tcl, there is no possibility to import proc's from other namespaces with other names to avoid collisions. For example, something like this:
namespace import_as ::private_namespace:my_proc1 my_proc2Using this command, there is a copy of my_proc1 with name my_proc2 in the current namespace
I strongly agree.
I've always imagined this as a new option to
[namespace import]:
namespace eval my {
namespace import -as bar ::orig::foo
}
so that then ::my::bar is an import of ::orig::foo
This is more direct and tidier than the
combination of [namespace import] and [rename]s
that is otherwise needed.
The only issue is that we're currently having namespace import working on a whole set of stuff defined by a list of patterns; this renaming-as-we-go only really works with a single command.
Not that it's a bad idea at all. Just needs documenting carefully.
In the meantime, this can be trivially implemented as a script:
proc import {link ns target} {
set tmpns [info cmdcount]
try {
namespace eval $tmpns [list namespace import ${ns}::*]
namespace eval $tmpns [list ::rename $target [
uplevel 1 {namespace current}]::$link]
} finally {
namespace delete $tmpns
}
return
}
Granted, importing all exported names is not so efficient, but it skirts pattern-matching issues.