Idea: "Frozen" inner function

Steve Horne stephenwantshornenospam100 at aol.com
Fri Nov 24 20:48:00 PST 2006


On Fri, 24 Nov 2006 21:59:33 +0100, Michael Butscher
<mbutscher at gmx.de> wrote:

>An inner function could get "frozen" as some kind of storage class 
>which means that all variables accessed by the inner function which are 
>defined in the surrounding function keep the value they had at the 
>point in time when the inner function was defined.

It's called a closure.

D inner functions seem to borrow a lot from the Pascal family
languages. These have inner functions, but don't have closures. It
simply isn't the done thing to call these inner functions using a
pointer after the outer function had exited - it isn't part of
structured programming.

Closures originally came about in languages that have 'first class
functions' - functional languages. These days, some scripting
languages have them - Python uses them for 'lambda' anonymous
functions, for instance.

But it is a trade-off between efficiency and flexibility. D currently
goes with efficiency by default, but you can explicitly create an
inner function with a closure if you need one - though its not very
convenient.

Tip - use an anonymous class with a function-call method.

Of course, a shorthand for doing this is a good idea.

-- 
Remove 'wants' and 'nospam' from e-mail.



More information about the Digitalmars-d mailing list