|Title:||Determining the "Type" of Commands|
|Version:||$Revision: 1.2 $|
|Author:||Donal K. Fellows <dkf at users dot sf dot net>|
|Created:||Wednesday, 31 July 2013|
|Keywords:||introspection, commands, Tcl, Tk|
This TIP describes a mechanism for determining what "type" of command a particular command is. This can be used as a prelude to performing other kinds of introspection, such as using info body, namespace origin or interp alias.
Currently, in order to find out information about an arbitrary command you have to apply a suitable introspection command and deal with any errors arising in order to tell that you had a command of some other type. It was made clear to me at EuroTcl 2013 that this was inelegant, especially since we in principle had the information available to do something neater.
The information in question is the pointer to the implementation function, that's stored in the C record describing the command. All that is needed is a way to surface that information to Tcl as a new introspection command.
This new introspection interface shall consist of one new subcommand of info and a pair of new public C functions.
info cmdtype commandName
The new info subcommand is to be called cmdtype (the name is chosen so as to not conflict with abbreviations info commands even though it does conflict with info cmdcount) and it takes a single argument, commandName, which must be the name of an existing Tcl command. The result of this subcommand shall be a string describing what sort of command commandName is; if no other information is available, the result shall be native.
NB: The Tcl implementation will not make any guarantees of the command type for any particular command supplied by default in any interpreter. User code should never assume that just because a command is implemented one way in one particular version that it will continue to be implemented that way in any future version.
The supporting public C functions shall be:
void Tcl_RegisterCommandTypeName(Tcl_ObjCmdProc *implementationProc, const char *nameStr)
const char * Tcl_GetCommandTypeName(Tcl_Command command)
Tcl_RegisterCommandTypeName will associate a particular implementation function, implementationProc, with an (assumed literal constant) string, nameStr; if nameStr is supplied as NULL, the mapping for implementationProc will be removed. The implementationProc argument must not be NULL. The use of a package prefix within the name is recommended.
Tcl_GetCommandTypeName shall take a command handle, command, and return the registered type name string (as previously passed to Tcl_RegisterCommandTypeName) for the command implementation function that the command is using. If there is no type name registered for the command's implementation function, the literal string native will be returned instead. The result will never be NULL.
The following command types are guaranteed to be among the set defined by default, but others may be done as well.
Procedures defined by the proc command.
Aliases defined by the interp alias command.
Ensembles defined by the namespace ensemble command.
Commands imported by the namespace import command.
Object (or class) defined by instantiating any TclOO class.
It is anticipated that Tk widget instances will report themselves through this mechanism as well, with a prefix to their names of tk::; that prefix should not be used by any other package. Note however that not all widget types will be distinguishable; this is part of the way that Tk is implemented.
The built-in widget creation functions may declare themselves to be of type tk::widgetFactory.
This document has been placed in the public domain.
[Index] [History] [Edit] [HTML Format] [Source Format] [LaTeX Format] [Text Format] [XML Format] [*roff Format (experimental)] [RTF Format (experimental)]TIP AutoGenerator - written by Donal K. Fellows