TIP 457: Add Support for Named Arguments

Login
Bounty program for improvements to Tcl and certain Tcl packages.
Tcl 2017 Conference, Houston/TX, US, Oct 16-20
Send your abstracts to tclconference@googlegroups.com
by Aug 21.
Author:         Mathieu Lafon <mlafon@gmail.com>
Author:         Andreas Leitgeb <avl@logic.at>
State:          Draft
Type:           Project
Vote:           Pending
Created:        21-Nov-2016
Post-History:   
Keywords:       Tcl,procedure,argument handling
Tcl-Version:    8.7

Abstract

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

Rationale

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 http://wiki.tcl.tk/10702 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.

Specification

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:

Introspection

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

Other use cases

Extended argument specifiers can also be used with other proc-like functions. The following functions are supported and can use extended argument specifiers:

Performance

The proposed modification has no significant performance impact:

When using named arguments specifiers to replace a similar handling done in Tcl-pure code, there is however a significant increase in performance.

See https://gist.github.com/mlafon/70480877a28f3571e0377eabc0cee7be for details on performance testing done on the proposed implementation.

Implementation

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 http://core.tcl.tk/tcl/timeline?r=tip-457 branch.

The code is licensed under the BSD license.

Discussion

This section details some of the alternate solutions for this feature or specific comments about it.

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 https://github.com/mlafon/tcl/tree/457-CALL-CMD . 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.

Some people have expressed concern about the modification of the proc command, which is a core command of Tcl. A particular attention has been paid to ensure that existing code will not be impacted and that future usage could be later added by adding new specifiers.

Argument Parsing command

Cyan Ogilvie's paper from Tcl2016 https://www.tcl.tk/community/tcl2016/assets/talk33/parse_args-paper.pdf describes a C extension to provide core-like argument parsing at speed comparable to proc argument handling, in a terse and self-documenting way.

Alexandre Ferrieux has proposed http://code.activestate.com/lists/tcl-core/18447/ to use the same argument specifiers than this proposal, but with a dedicated command which can be called from the proc body. This has the advantage to not alter the proc command and could be located in an extension.

Although the proc usage will not be modified, this new command will probably have to access or modify internal proc structures, for example to support introspection.

Having to declare final local variables in the body, also seems confusing for users.

Preventing Data-dependent bugs

It has been proposed by Christian Gollwitzer http://code.activestate.com/lists/tcl-core/18457/ to make the special '--' end-of-options marker mandatory when the number of positional arguments after the named group is not fixed. This would suppress any potential Data-Dependent bugs related to the search of the initial dash and remove any unwanted object stringification, at the expense of forcing the user to explicitely use the end-of-option marker.

This proposal is currently not implemented but the documentation has been modified to list the cases for which '--' should be use.

Copyright

This document has been placed in the public domain.

History