TIP #457: Add Support for Named Arguments

Title:Add Support for Named Arguments
Version:$Revision: 1.19 $
Authors: Mathieu Lafon <mlafon at gmail dot com>
Andreas Leitgeb <avl at logic dot at>
Created:Monday, 21 November 2016
Keywords:Tcl, procedure, argument handling


This TIP proposes an enhancement of the Tcl language to support named arguments and additional features when calling a procedure.


The naming of arguments to procedures is a computer language feature which allow developers to specify the name of an argument when calling a function. This is especially useful when dealing with arguments with default values, as this does not require to specify all previous arguments when only one argument is required to be specified.

As such, this is a commonly requested feature by Tcl developers, who have created various code snippets [1] to simulate it. These snippets have drawbacks: not intuitive for new users, require to add extra code at the start of each procedure, no standard on the format to use, few errors handling, etc.

After discussing various possibilities with the community, it has been decided to extend the argument specification of the proc command and allow users to define options on arguments. This can be used to support named arguments but also add additional enhancements: flag arguments, pass-by-name (upvar) arguments, non-required arguments, ...

The others possibilities discussed are detailed in the Discussion section at the end of the document.


The proc documentation currently define argument specifiers as a list of one or two fields where the first field is the name of the argument and the optional second field is its default value.

The proposed modification is to support an alternate specifier format where the first field is also the name of the argument, followed by a paired list of options and their values. This format does not prevent the original format to be used as they can be easily distinguished: the new format uses an odd size list with a minimal size of three fields.

Available argument specifiers

The following argument specifiers are defined in this TIP:

Further argument specifiers may be added in future TIP. Examples of new argument specifiers which may be added in the future:

Named arguments

The following rules define how named arguments are expected to be specified on the call-site:

Generated usage description

The error message, automatically generated when the input arguments are invalid, is updated regarding new options:


The info argspec proc command is added to get the argument specification of all arguments or of a specific argument.

% proc p { a { b 1 } { c -name c } } {}
% info argspec proc p
a { b -default 1 } { c -name c }
% info argspec proc p c
-name c

Similar info argspec subcommands are also added for lambda, object method and object constructor.

The info argspec specifiers command is added to get the specifiers supported by the current interpreter.

% info argspec specifiers
-default -name -required -switch -upvar -varname


The performance of the call of a Tcl-pure procedure defined without any extended argument specifiers is not affected by the proposed modification because the original initialization code is still used in that case.

The call of a Tcl-pure procedure defined with extended argument specifiers uses a different initialization code which may slightly affect performance. Initial testing only show a slowdown between 0% and 4%. See [2] for details.

Users with critical requirements on speed should be warned and may prefer not using extended argument specifiers or may want to test the performance on their specific usage first.


This document proposes the following changes to the Tcl core:

  1. Add ExtendedArgSpec structure which is linked from CompiledLocal and contains information about extended argument specification;

  2. Add a flags field in the Proc structure to later identify a proc with at least one argument defined with an extended argument specification (PROC_HAS_EXT_ARG_SPEC);

  3. Update proc creation to handle the extended argument specification and fill the ExtendedArgSpec structure;

  4. Update InitArgsAndLocals to initialize the compiled locals using a dedicated function if the PROC_HAS_EXT_ARG_SPEC flag has been set on the proc. If unset, the original initialization code is still used.

  5. Update ProcWrongNumArgs to generate an appropriate error message when an argument has been defined using an extended argument specification;

  6. Add info argspec command;

  7. Update documentation in doc/proc.n and doc/info.n;

  8. Update impacted tests and add dedicated tests in tests/proc-enh.test.

Reference Implementation

The reference implementation is available in the tip-457 [3] branch.

The code is licensed under the BSD license.


This section details some of the envisioned solutions for this feature.

Initial approaches that tried to work with unmodified procedures are not detailed here for clarity.

Dedicated builtin command

A dedicated command can be used to handle the named arguments, using an -option value syntax, before calling the target procedures with all arguments correctly prepared.

% call -opts myproc -optC foo -optB {5 5} -- "some pos arg"

An implementation of this proposal is available at [4]. This proposal was abandoned as it was not enough intuitive for users.

Modification in how proc are defined

Tcl-pure procedures can be defined in a way which state that the procedure will automatically handle -option value arguments.

% proc -np myproc { varA { optB defB } { optC defC } { optD defD } args } { .. }
% myproc -optC foo -optB {5 5} -- "some pos arg"

An other possibility is to support options on arguments and allow name specification:

% proc myproc { varA { optB -default defB -name B } args } { .. }
% myproc a -B b zz

This is the currently proposed solution in this TIP. It requires the procedures to be modified but allow additional features.

Argument Parsing extension

Cyan Ogilvie's paper from Tcl2016 [5] describes a C extension to provide core-like argument parsing at speed comparable to proc argument handling, in a terse and self-documenting way.


This document has been placed in the public domain.

Powered by Tcl[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