Tcl Medium · Lesson 1 · On the house

Namespaces in depth: export, import, path and ensemble maps

Take qualified names apart, share commands with export and import, resolve names through namespace path, and map ensemble subcommands to any command. Lesson 1 is free — on the house.

Lessons · Tcl Medium · Lesson 1 of 10 · On the house

Namespaces in depth: export, import, path and ensemble maps

Take qualified names apart, share commands with export and import, resolve names through namespace path, and map ensemble subcommands to any command.

Illustration for Tcl
Lesson 1 is on the house.

namespace current returns the fully qualified name of the namespace the code is running in. namespace qualifiers ::geo::areaRect returns ::geo and namespace tail returns areaRect; both work on the string alone, so the namespace does not have to exist.

namespace export area* perimeter lists, with glob patterns, the commands other namespaces may import. namespace import ::geo::area* then creates linked commands in the importing namespace for the exported matches only, so the unexported helper never comes across. namespace origin follows an imported command back to the real one.

namespace path ::geo creates no commands. It makes the namespace look up unknown command names in ::geo after itself and before the global namespace. Command lookup order is: current namespace, each namespace on its path, then ::. Variables never follow the path.

namespace which -command perimeter returns the full name a command word resolves to from the current namespace, or an empty string when it resolves to nothing. It is the quick way to debug a lookup that finds the wrong proc.

namespace ensemble create -command ::shape -map {sq areaSquare ...} builds an ensemble with its own command name, where each subcommand maps to any command. An unknown subcommand raises an error that lists the valid ones.

namespace code script wraps a script so that it later runs inside the namespace that created it, with any extra words appended as arguments. Use it for callbacks given to after, trace or chan event, which otherwise run at the global level.

namespace delete ::calc removes the namespace with every command and variable in it, and namespace exists checks for one.

namespaces in depth

namespace eval ::geo {
    namespace export area* perimeter
    proc areaSquare {s} { expr {$s * $s} }
    proc areaRect {w h} { expr {$w * $h} }
    proc perimeter {w h} { expr {2 * ($w + $h)} }
    proc helper {} { return hidden }
    proc where {} { namespace current }
}
puts [geo::where]
puts [namespace qualifiers ::geo::areaRect]
puts [namespace tail ::geo::areaRect]
namespace eval ::app {
    namespace import ::geo::*
    proc run {} { areaRect 3 4 }
}
puts [::app::run]
puts [lsort [info commands ::app::*]]
puts [namespace origin ::app::areaSquare]
namespace eval ::calc {
    namespace path ::geo
    proc go {} { perimeter 2 5 }
}
puts [::calc::go]
puts [namespace eval ::calc {namespace which -command perimeter}]
puts [llength [namespace which -command perimeter]]
namespace eval ::geo {
    namespace ensemble create -command ::shape -map {
        sq areaSquare rect areaRect per perimeter
    }
}
puts [shape sq 6]
puts [shape per 1 2]
puts [catch {shape tri 1} msg]
puts $msg
set cb [namespace eval ::geo {namespace code {areaRect 2}}]
puts [{*}$cb 9]
namespace delete ::calc
puts [namespace exists ::calc]
Match this when you type.

Quiz

After namespace import ::geo::*, why is helper not available in ::app?

Quiz

What does namespace path ::geo add to ::calc?

Quiz

What does namespace which -command perimeter return when nothing matches?

Quiz

What does the -map option of namespace ensemble create do?

Check

Match the still for: Namespaces in depth: export, import, path and ensemble maps.

Open-book: the answers are on this page. Pass every quiz and check (5) to mark the lesson done. This visit: 0/5.