 | Robert A Duff wrote: > Bernd Paysan writes: [...] > > I'm pretty sure that function pointers do not exist at all in the original > > Pascal... > > True, but the original Pascal allows one to pass a procedure or function > as a parameter, which allows some of the power supported by both GCC-C > and Ada 2005. It's not called a pointer in Pascal, but of course the > implementation involves pointers in some way. What you can't do in > Pascal is store a procedure (or pointer to procedure) in some data > structure, and call it later. That is, when you pass a procedure as a > parameter, the only things the callee can do is call it and pass it > along to another procedure -- you can't assign it into a variable, > or return it to a more global (longer lived) place.
And this is an important restriction (for implementation efficiency), because it guarantees that the lifetime of a closure is _never_ outside the lifetime of the stack frame for the function call environment it references. It would be OK to also permit assigning them to local variables. The important part is that no closures escape upward in the call chain. And this lets you keep everything on the stack.
[...]
> >... He did what he did, with his Lisp > > background the result is easy to understand. > > Well, if he were mimicing Lisp, he would have to allow nested functions > to outlive their container, which is a whole 'nother thing!
Closures which can outlive the stack frame in which they were created require garbage collection / reference counting. They have to be allocated on the heap. And if you really need that many closures floating around, perhaps C is not the language of choice here. When you're programming in a really functional style, a LISP/Scheme/ML/etc.. compiler is going to be better at dealing with those control flow patterns.
--Eric
|
|