Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2021-05-04
12:59 • Ticket [c9ca7bca7e] import proc from namespace with other name status still ... artifact: 987135153f user: pooryorick
2016-10-09
15:46 • Ticket [c9ca7bca7e]: 4 changes artifact: cfdd2a744c user: dkf
2016-09-18
19:21 • Ticket [c9ca7bca7e]: 4 changes artifact: 8cef93a49e user: dgp
00:17 • New ticket [c9ca7bca7e]. artifact: 6009146fa7 user: anonymous

import proc from namespace with other name

Openc9ca7bca7e22925cddf02d74e39dd043a28bd167Created 10 years agoUpdated 6 years ago
Descriptionhtml2016-09-18 00:17:44

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_proc2
Using this command, there is a copy of my_proc1 with name my_proc2 in the current namespace

dgpplain2016-09-18 19:21:36
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.
dkfwiki2016-10-09 15:46:37

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.

pooryorickwiki2021-05-04 12:59:01

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.

Log in (anonymous is fine) to add a comment.