For Programmers: Free Programming Magazines  


Home > Archive > Tcl > February 2005 > Re: A different design for closures









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Re: A different design for closures
antirez

2005-02-23, 8:59 am

antirez wrote:
> proc createAdder {x} {
> lamba {y} {expr $y+[closure get $x]} [list x $x]
> }


I forgot to include a couple of points.

The closure command can also access closures of external procedures:

[closure in $procName get/set x ...] and so on.
This makes possible to share the environment if really needed.
Example:

proc createAdder {x} {
set a [lamba {y} {expr $y+[closure get $x]} [list x $x]]
set b [lamba {incr} {closure in $a set x $incr} [list a $a]]
list $a $b
}

this returns one closure to add and one to change the increment
performed by the first closure.

Also it is worth to note that this way to do closures don't play
well with functions as values nor with lambas based on auto expansion,
because full closures need to be able to modify their environment
so they can not be immutable values.

Ciao,
Salvatore
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com