beetlebug▾
/

namespace import

Makes exported commands of other namespaces callable in the current namespace by their short names.

namespace import ?-force? ?PATTERN ...?

Description

Each PATTERN is a name with a namespace, such as ::m::add or m::*. The last part is a glob pattern. For each exported command of that namespace that matches, namespace import makes a command of the same short name in the current namespace that calls it. A command that is not exported is not imported, and gives no error.

A command of the same name that is already in the current namespace is an error, unless -force. With no PATTERN, it gives the names of the imported commands of the current namespace.

Arguments

ArgumentTypeDefaultDescription
PATTERNqualified glob patternnoneThe namespace and the names of the commands to import.

Options

OptionTypeDefaultDescription
-forceflagnoneReplaces a command of the same name.

-force

Replaces a command of the same name.

With -force, an imported command replaces a command of the same short name in the current namespace.

% namespace eval app {proc add {} {return mine}; namespace import -force ::m::add; add 2 2}
4

A flag has no value to check.

Returns

An empty string, or the imported commands with no PATTERN.

Examples

% namespace eval m {proc add {a b} {expr {$a + $b}}; proc _priv {} {}; namespace export add}
> namespace eval app {namespace import ::m::add; add 1 2}
3

% namespace eval app {namespace origin add}
::m::add

# _priv is not exported.
% namespace eval app {namespace import ::m::_priv; info commands _priv}

Errors

% namespace import nope::*
unknown namespace in import pattern "nope::*"
-errorcode TCL LOOKUP NAMESPACE

% namespace eval app {proc mine {} {}}; namespace eval m {proc mine {} {}; namespace export mine}; namespace eval app {namespace import ::m::mine}
can't import command "mine": already exists
-errorcode NONE

Facts

Capability
None.
Tcl release
Tcl 9.1 and 8.6.
Since
ztcl 1.0.0-beta.1

See also