Version 49 of Creating Commands

Updated 2010-04-20 13:55:36 by CMcC

Tcl provides several commands which create commands and script evaluation contexts.

The purpose of this page is to enumerate and classify those commands.

  • [proc] creates a Tcl script based procedure (function) which can be defined to have a fixed or variable number arguments, with default values for the fixed arguments.
  • [apply] creates an anonymous command
  • [interp create] creates a named slave interp command
  • [interp alias] creates a named alias to another command
  • [thread create] creates an opaque token associated with a thread
  • [namespace ensemble] creates a namespace command from a namespace
  • [class create] creates a named class command
  • [$class create] creates an opaquely named object instance command
  • [coroutine] creates a named coroutine command
  • Tcl_CreateObjCommand is the C API for creating a command object in a specific Tcl interpreter.

generatorcmd?#argsinvocationdestructorrename?
procyesanyby nameyes
coroutineyes1by nameyes
Tcl_CreateObjCommandyesanyby nameyes
namespace ensembleyesanyby namenamespace deleteyes
class createyesanyby name$class destroyyes
$class createyesanyby name$obj destroyyes
interp aliasyesanyby nameinterp aliasyes
interp createyesany$interp eval$interp destroyyes
thread::createnoanythread::sendthread::releaseno
applynoanyby referenceimplicitno

key
generatorwhat command generates an instance of this form?
cmd?does this form construct a command?
#argshow many args does the constructed form take?
invocationhow is this form invoked?
destructorwhat explicit destructor disposes of this form?
rename?does [rename $name {}] destroy this form?

Observations

The generation of commands appears to be a means of invoking the form and of controlling the resources associated with the form.

There are two exceptions: In the case of apply, resources are associated with a value, and the lifetime of the generated form is tied to that of the value. In the case of thread, explicit refcounting is emplyed. In all other cases [rename $name {}] destroys both the command and its associated form.

Irregularities

interp-created commands duplicate functionality available through interp, to control interp resources. It would be more consistent with the other forms if $interp args were passed directly into the interp as if [interp eval]'d.

coroutine-created commands are the only forms restricted to single args.