How Nested Functions Work, part 1
Jeremie Pelletier
jeremiep at gmail.com
Mon Aug 31 11:42:09 PDT 2009
Walter Bright Wrote:
> language_fan wrote:
> > This seems more like an advertisement of D than practical new information
> > for compiler construction. Nesting functions is the basic feature of
> > functional languages. Moreover even procedural Algol based languages such
> > as Pascal have always supported them, too.
>
> But not C, C++, Java, etc., so quite a lot of programmers have little
> experience with them, and even less understanding.
>
> > This information is also
> > taught in basic university level compiler courses.
>
> I bet only a tiny fraction of programmers have taken university compiler
> classes. (Also, they were not covered in compiler classes I took.)
> Consider also that the Java JVM doesn't support static links, and last I
> heard the .NET IL didn't, either.
>
>
> > Now that I checked what wikipedia had to say to the matter, it also
> > seemed to mention D. Apparently 'c-like syntax' plus 'advanced feature
> > <foo>' always equals 'innovation'.
>
>
> Nested functions aren't innovative; they just are apparently lacking in
> many other popular languages, and seem to confuse a lot of people. If
> you google it, you'll find there's a lot of programmer confusion about
> them. Hence an article as to how they work is in order.
>
> In part 2, I'll cover innovative things D does with nested functions.
I agree with Walter here. While D is not the first place I see closures and nested functions, it is the first language I come across that blends delegates, closures and nested functions in a simple, elegant and intuitive manner.
PHP for one does not support closures at all, and delegates are done using the ugly array(object => 'method') syntax. JavaScript works like C#; you have to assign a closure to a delegate value to get a nested function. C/C++ also don't support closures, and delegates must be manually handled (function ptr + void* param) with very little support from many existing APIs (win32 is especially bad for that as it only uses static callbacks).
Its no surprise that I was literally drooling when I first read about how D does it, I had just spent a few weeks doing jQuery in JavaScript for a client back then and fell in love with closures, D had it all with better syntax :)
In D you can even send a function pointer to a delegate value!
More information about the Digitalmars-d
mailing list