April 21st, 2025

Adding keyword parameters to Tcl procs

The article introduces a custom `proc*` command in Tcl to implement keyword parameters, addressing limitations in standard commands and discussing the use of quasiquoting and challenges in metaprogramming.

Read original articleLink Icon
Adding keyword parameters to Tcl procs

The article discusses the addition of keyword parameters to Tcl procedures, addressing a common frustration among programmers regarding the lack of named and order-agnostic parameters in some programming languages. The author highlights that while Tcl has UNIX-like options in its standard commands, these features are not available in the `proc` or `apply` commands. The article presents a custom implementation of a `proc*` command that allows for keyword parameters, demonstrating its functionality with examples. The author also explains the underlying mechanics of this implementation, including the use of quasiquoting for code generation, which is necessary due to Tcl's limitations in metaprogramming. The article concludes with a reflection on the challenges of using regular expressions for metaprogramming in Tcl, emphasizing the complexity involved in creating a flexible and functional system for handling keyword parameters.

- Tcl lacks built-in support for keyword parameters in its standard commands.

- The author introduces a custom `proc*` command to implement keyword parameters.

- The implementation includes examples demonstrating the functionality of keyword parameters.

- Quasiquoting is used for code generation due to Tcl's metaprogramming limitations.

- The article reflects on the challenges of using regular expressions for metaprogramming in Tcl.

Link Icon 5 comments
By @jrapdx3 - 18 days
As a search of the Tcl wiki shows, attempts to add named-argument capability to the Tcl proc command have been around for several years.

My own nxproc extension is more comprehensive. (See wiki [0].)

The extension enables named arguments, regular positional arguments, and 'rest' arguments, nxargs and nxunknown. Nxproc also provides (optional) type-checking of procedure arguments. (Types: string, number, bool and enumerated. Enums are lists of values restricting what the arg can contain.)

Nxproc supports TclOO with nxcontructor and nxmethod commands -- same feature set as "plain" nxproc. Also provides case-insensitive '-ci' variants, and runtime display of named-argument default/actual values and types.

Nxproc is a Tcl C extension. Bundle has Windows, Linux binaries. Compiles easily on other platforms.

[0] https://wiki.tcl-lang.org/page/nxproc

By @blacksqr - 18 days
The OpenACS web server toolkit has a lot of useful Tcl utilities, including the ad_proc procedure, which wraps proc and allows adding of switches, inline docs, and more.

I think it would be very useful to turn ad_proc into a built-in command and incorporate it into TCL.

https://nstcl.sourceforge.net/docs/nstcl-core/ad_proc.html

By @tracnar - 18 days
I implemented something very similar a while ago, it's indeed too bad it's not built-in. I don't think you need such a "quasiquote" function, [list {*}$args] can escape a single command, and then it's a matter of joining multiple commands using a newline. IIRC that's how I did it.

I also had further fun with wrapping "proc" by implementing a "pyproc" which called out to Python while looking like a normal Tcl proc.

By @andrewshadura - 18 days
Tcl is the shell done right. Simple, logical, consistent.
By @RicoElectrico - 18 days
Synopsys tools (either CosmosScope or Custom Wave view) do it by the way of names following values. Like:

    measureFoo $signal threshold 0.9 edge last