[fpc-devel] Nested function closures

Ryan Joseph genericptr at gmail.com
Tue Apr 27 17:44:00 CEST 2021

Continued from our discussion at https://bugs.freepascal.org/view.php?id=24481.

> if the compiler devs will allow me as soon as this is finished I want to allow the existing nested functions functionality to work with anonymous functions, so at the very least we don't need to generate the expensive interface based object which often times is not even needed. At that point we would need to make nested functions inline-able, which they are currently not. But we're not there yet so lets not complicated anything by proposing extensions to a feature that doesn't even exist yet.

Sven replies:

> Getting rid of the interface only works in very narrow circumstances that are so seldom in real world code that it is not worth the effort.

I'm referring to my test I did a few years ago (https://github.com/graemeg/freepascal/compare/master...genericptr:anon_funcs) where I say we can use existing nested functions as a closure when passing is not required. As you can see I already implemented this quite easily but it is not related to the new forthcoming closures feature. I did in fact try to replace the interface with a record on the old closures branch but I ran into many problems I decided it wasn't the best route.

Indeed there are many times where we don't want a heap allocated interface you can pass around  but rather a simple inline function pointer like below. Consider this loop is run 60 times a second and allocating a useless class every time for no gain. This could easily be 1000*60=60,000 constructions and allocations of a class.

  for i := 0 to entities.Count - 1 do
      value := entities[i];
      value.SortEntities(function(a, b: TEntity): integer
          // do stuff

So anyways what I propose is if a closure is never passed outside of scope (i.e. temporary) then use anonymous nested functions instead (like in my GitHub branch). If this is an acceptable approach I will personally do what is required to get it implemented along side the real closures.

	Ryan Joseph

More information about the fpc-devel mailing list