Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2016-07-14
07:26 • Ticket [1932618fff] Enhance proc for anonymous functions and closures stat... artifact: 56c9c2542f user: dkf
2016-07-12
19:27 • Ticket [1932618fff]: 6 changes artifact: 6b311c70d3 user: dkf
2008-04-05
20:08 • Ticket [1932618fff]: 5 changes artifact: 11f316b388 user: msofer
2008-04-03
03:24 • Ticket [1932618fff]: 4 changes artifact: f2ee65ceb6 user: andreas_kupries
2008-04-02
20:20 • New ticket [1932618fff]. artifact: 48f20270be user: ecky-l

Enhance [proc] for anonymous functions and closures

Open1932618fffffffffffffffffffffffffffffffffCreated 19 years agoUpdated 10 years ago
Descriptionplain2008-04-02 20:20:05
I would like to request that [proc] becomes a data type in addition to a command, so that procs can be


- created on the fly, capturing the current environment and procedure stack
- created anonymously, e.g. [proc {} {...}] would return a new proc to be used elsewhere.
- returned by other proc's
- given as arguments to other procs (including their arguments and the environment in which they were created)

When this s possible, I think it would also be possible to have closures in Tcl and better capabilities for functional programming - which would enhance the language *a lot*

Is it possible to have [proc] as a Tcl_Obj* type?
andreas_kupriesplain2008-04-03 03:24:17
Logged In: YES 
user_id=75003
Originator: NO

Is TIP #194: Procedures as Values via '''apply''' (http://tip.tcl.tk/194) what you seek ?
If yes then please try out Tcl 8.5 there this is implemented.
msoferplain2008-04-05 20:08:18
Logged In: YES 
user_id=148712
Originator: NO

Taking ownership as it is close to other things on my plate. See also clt thread at http://groups.google.com/group/comp.lang.tcl/browse_thread/thread/ffa48b75264ae4db/0226ae5e1f6bd115?lnk=raot#0226ae5e1f6bd115
dkf Ticket [1932618fff]: 6 changes [History diff]2016-07-12 19:27:38
dkfwiki2016-07-14 07:26:05

This is genuinely tricky to do without introducing some whopper incompatibilities at the deep semantic level. In particular, "capturing the current environment" is difficult without causing deep trouble with values that are not properly serializable (unlike all current Tcl-defined values). The problem is that proc creates commands, and command handles are single values with an expectation that they can be used transparently.

However, you can do a lot with coroutine and apply without tackling the deeply difficult parts. You can also do a lot more if you're creating a method and not a procedure call; the extra context (and the expectation of calling the code via an object handle) make a big difference. Finally, you also gain options if you drop the expectation of being able to modify captured variables and have those modifications persist. (There are also issues relating to what the expected captured stack would look like; I really recommend not trying to capture that without a coroutine!) For example, if you drop being usable a single value and modifying the captures, you can do it with apply and some creative scripting.

Log in (anonymous is fine) to add a comment.