Adding keyword parameters to Tcl procs

by BoingBoomTschakon 4/21/25, 4:55 PMwith 17 comments
by jrapdx3on 4/21/25, 8:36 PM

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 blacksqron 4/21/25, 7:06 PM

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 tracnaron 4/21/25, 6:24 PM

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 andrewshaduraon 4/21/25, 6:21 PM

Tcl is the shell done right. Simple, logical, consistent.

by RicoElectricoon 4/21/25, 10:30 PM

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