TIP #284 Version 1.9: New 'invoke' and 'namespace invoke' Commands

This is not necessarily the current version of this TIP.


TIP:284
Title:New 'invoke' and 'namespace invoke' Commands
Version:$Revision: 1.9 $
Author:Miguel Sofer <msofer at users dot sourceforge dot net>
State:Draft
Type:Project
Tcl-Version:8.5
Vote:Pending
Created:Sunday, 01 October 2006

Abstract

This TIP exposes a Tcl script-level interface to the direct command invokation engine already present in the Tcl library for years.

Proposed New Commands

This TIP proposes a new subcommand to namespace, invoke, with the following syntax:

namespace invoke namespace cmd ?arg ...?

This invokes the command called cmd in the caller's scope, as resolved from namespace namespace, with the supplied arguments. If namespace does not exist, the command returns an error.

This TIP also proposes a new command:

invoke level cmd ?arg ...?

This invokes the command cmd in level level with the supplied arguments. The syntax of level is the same as that required by uplevel.

Rationale

There is currently no script-level equivalent to Tcl_EvalObjv(), though the functionality is provided by one of:

   eval [list cmd ?arg ...?]
   {expand}[list cmd ?arg ...?]

Note that the core tries to optimise the first case, but has to be careful to only avoid reparsing when it is guaranteed safe to do so. The notation is rather clumsy too.

The proposed new commands try to improve this situation, with the added functionality of determining the namespace in which the command name is to be resolved (functionality which was very difficult to use previously using the script-level API). In this manner it is possible for the invocation to make good use of namespace path and namespace unknown features.

The new command invoke could be implemented in 8.5 as:

   set lam [list {level cmd args} {
       if {[string is integer $level] && ($level >= 0)} {
           incr level
       }
       uplevel $level $args
   }]
   interp alias {} invoke {} ::apply $lam

which helps to illustrate the use of invoke: to simplify these uplevel+list tricks.

Reference Implementation and Documentation

[RFE 1577324] (which depends on [Patch 1577278]) provides an implementation of namespace invoke.

Differences to other commands

  1. Both these commands perform command invocation, as opposed to the script evaluation done by eval, uplevel and namespace eval

  2. namespace inscope does a magic expansion of the first command argument, namespace invoke takes the first command argument as a command name. In other words, it is not a really a command but rather a command prefix. Feedback on the semantics suggest that this is a worthy feature, very useful for packing up command prefixes. This tip may yet be revised or withdrawn to take that into consideration.

  3. both namespace eval and namespace inscope add a call frame,namespace invoke does not - it invokes in the caller's frame

Copyright

This document has been placed in the public domain.


Powered by TclThis is not necessarily the current version of this TIP.

TIP AutoGenerator - written by Donal K. Fellows