Tcl_Namespace(3)
Tcl_Namespace(3) โ Tcl Library Procedures โ manipulate namespaces ๐ โจ
okay bestie, welcome to the namespace corner of the Tcl C API. This is Tcl_Namespace(3), and it is literally your group chat for making, finding, importing, exporting, and generally slaying with namespaces. No cap, every function below is here to help you boss around namespaces from C. ๐ซถ
๐ NAME ๐
Tcl_AppendExportList, Tcl_CreateNamespace, Tcl_DeleteNamespace, Tcl_Export, Tcl_FindCommand, Tcl_FindNamespace, Tcl_ForgetImport, Tcl_GetCurrentNamespace, Tcl_GetGlobalNamespace, Tcl_GetNamespaceUnknownHandler, Tcl_Import, Tcl_SetNamespaceUnknownHandler โ manipulate namespaces. ๐
โจ SYNOPSIS โจ
#include <tcl.h>
Tcl_Namespace *
Tcl_CreateNamespace(interp, name, clientData, deleteProc)
Tcl_DeleteNamespace(nsPtr)
int
Tcl_AppendExportList(interp, nsPtr, objPtr)
int
Tcl_Export(interp, nsPtr, pattern, resetListFirst)
int
Tcl_Import(interp, nsPtr, pattern, allowOverwrite)
int
Tcl_ForgetImport(interp, nsPtr, pattern)
Tcl_Namespace *
Tcl_GetCurrentNamespace(interp)
Tcl_Namespace *
Tcl_GetGlobalNamespace(interp)
Tcl_Namespace *
Tcl_FindNamespace(interp, name, contextNsPtr, flags)
Tcl_Command
Tcl_FindCommand(interp, name, contextNsPtr, flags)
Tcl_Obj *
Tcl_GetNamespaceUnknownHandler(interp, nsPtr)
int
Tcl_SetNamespaceUnknownHandler(interp, nsPtr, handlerPtr)
๐ ARGUMENTS ๐
Tcl_Interp *interp(in/out) โ the interpreter where the namespace exists and where name lookups happen. It's also where error result messages get written. main character interpreter energy.const char *name(in) โ the name of the namespace or command you're creating or accessing.void *clientData(in) โ a context pointer from whoever created the namespace. Tcl does not interpret it at all.Tcl_NamespaceDeleteProc *deleteProc(in) โ a pointer to the function to call when the namespace is deleted, orNULLif you don't want any such callback.Tcl_Namespace *nsPtr(in) โ the namespace to be manipulated, orNULL(for everything exceptTcl_DeleteNamespace) to manipulate the current namespace.Tcl_Obj *objPtr(out) โ a reference to an unshared value where the function output will be written.const char *pattern(in) โ the glob-style pattern (seeTcl_StringMatch) that describes the commands to be imported or exported.int resetListFirst(in) โ whether the list of export patterns should be reset before adding the current pattern to it.int allowOverwrite(in) โ whether new commands created by this import action can overwrite existing commands.Tcl_Namespace *contextNsPtr(in) โ the spot in the namespace hierarchy where the search for a namespace or command should happen relative to, when the search term is not rooted at the global namespace.NULLmeans the current namespace.int flags(in) โ an OR-ed combination of bits controlling how the search is performed. The supported flags are:TCL_GLOBAL_ONLYโ the search is always conducted relative to the global namespace.TCL_NAMESPACE_ONLYโ just forTcl_FindCommand; the search is always conducted relative to the context namespace.TCL_LEAVE_ERR_MSGโ an error message should be left in the interpreter if the search fails.
Tcl_Obj *handlerPtr(in) โ a script fragment to be installed as the unknown command handler for the namespace, orNULLto reset the handler to its default.
๐ธ DESCRIPTION ๐ธ
Namespaces are hierarchic naming contexts that can contain commands and variables. They also maintain a list of patterns that describes what commands are exported, and they can import commands that have been exported by other namespaces. Namespaces can also be manipulated through the Tcl command namespace. iykyk.
The Tcl_Namespace structure encapsulates a namespace, and it is guaranteed to have these fields in it:
nameโ the local name of the namespace, with no namespace separator characters in it, and with empty denoting the global namespace.fullNameโ the fully specified name of the namespace.clientDataโ the value specified in the call toTcl_CreateNamespace.deleteProcโ the value specified in the call toTcl_CreateNamespace.parentPtrโ a pointer to the containing namespace, orNULLfor the global namespace.
Tcl_CreateNamespace creates a new namespace. The deleteProc will have this type signature:
typedef void Tcl_NamespaceDeleteProc(
void *clientData);
Tcl_DeleteNamespace deletes a namespace, calling the deleteProc defined for the namespace (if any). so real, so final.
Tcl_AppendExportList retrieves the export patterns for a namespace given namespace and appends them (as list items) to objPtr.
Tcl_Export sets and appends to the export patterns for a namespace. Patterns are appended unless the resetListFirst flag is true.
Tcl_Import imports commands matching a pattern into a namespace. Note that the pattern must include the name of the namespace to import from. This function returns TCL_ERROR if an attempt to import a command over an existing command is made, unless the allowOverwrite flag has been set.
Tcl_ForgetImport removes imports matching a pattern.
Tcl_GetCurrentNamespace returns the current namespace for an interpreter.
Tcl_GetGlobalNamespace returns the global namespace for an interpreter.
Tcl_FindNamespace searches for a namespace named name within the context of the namespace contextNsPtr. If the namespace cannot be found, NULL is returned.
Tcl_FindCommand searches for a command named name within the context of the namespace contextNsPtr. If the command cannot be found, NULL is returned.
Tcl_GetNamespaceUnknownHandler returns the unknown command handler for the namespace, or NULL if none is set.
Tcl_SetNamespaceUnknownHandler sets the unknown command handler for the namespace. If handlerPtr is NULL, then the handler is reset to its default. boom, handled.
๐งฎ REFERENCE COUNT MANAGEMENT โจ
okay bestie, gather round, this is the part where the refcounts get spicy and if you fumble it your program will simply pass away ๐ so listen up fr fr
- the
objPtrargument toTcl_AppendExportListโ you gotta hand it an unshared object, okay? not shared, not "it's fine nobody else is looking at it". because this function is gonna modify it. and heads up: the reference count ofobjPtrwill not be altered. like, at all. so don't be counting on it changing, because it won't ๐ Tcl_GetNamespaceUnknownHandlerโ this one hands you back a value that might be shared. so if you're planning to keep it around (retain it, hold onto it, make it yours ๐), you need to increment its reference count yourself. it's giving "you break it, you bought it" energy but for refcounts ๐ซถ- the
handlerPtrargument toTcl_SetNamespaceUnknownHandlerโ here's the tea: its reference count will get incremented, but only if it's a non-empty list. empty list? no bump. non-empty list? bump. that's the rule, no cap โญ
๐จ lowkey important: these aren't suggestions, they're the rules. get them wrong and you're in undefined-behaviour territory and nobody wants that for you ๐
๐ SEE ALSO ๐
go read these next, they're the supporting cast and they ate:
Tcl_CreateCommand(3)Tcl_ListObjAppendList(3)Tcl_SetVar(3)
๐ KEYWORDS ๐
the tags, the vibe check, the search terms:
- namespace
- command
and that's the whole page, bestie!! this one came from the Tcl crew, page Tcl_Namespace(3), version 8.5 โ iconic behaviour honestly, she's been serving since forever and she's still slaying ๐๐
โจ
๐ฆ where this page comes from
- alpine 3.23: tcl9-doc 9.0.3-r0 ยท TCL
- alpine 3.24: tcl9-doc 9.0.3-r2 ยท TCL
- fedora 43: tcl-doc 1:9.0.2-1.fc43 ยท TCL AND GPL-3.0-or-later WITH Bison-exception-2.2 AND BSD-3-Clause
- fedora 44: tcl-doc 1:9.0.2-1.fc44 ยท TCL AND GPL-3.0-or-later WITH Bison-exception-2.2 AND BSD-3-Clause
- nixos 26.05: tcl 9.0.3
- Copyright (c) 2003 Donal K. Fellows
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms