Closures issues

Andrei Khropov andkhropov at nospam_mtu-net.ru
Sat Jul 22 15:51:22 PDT 2006


Hello everybody.

There's a reoccuring issue with closures, i.e. downwards FunArg problem
(http://en.wikipedia.org/wiki/Funarg_problem#Downwards_funarg_problem).

In simple terms delegates that contain pointer to function that has exited are
actually point to some garbage. This leads to UB.

So I would like to clarify situation a bit:

1) I.e. should D support full closures (like functional languages do)?
   Solution is actually simple - allocate local variables that are used by a
delegate in a heap object 	(of course only if it is actually necessary)
  ( like here: http://www.digitalmars.com/drn-bin/wwwnews?digitalmars.D/39525 ).
   This will actually enable truly functions as first-class objects and 
   higher-order functions
   (because we could return delegates from functions) 

   That would be great (IMHO).

   Of course there are some issues with RAII objects - when should they be
destroyed - after frame is collected or delegates should simply be prohibited
to use them.

2) If D is not intended to support full closures:

   2.1) Prohibit return of delegates with stack frames

   2.2) there should be a mechanism to detect this kind of errors (at least in
Debug).
A sort of garbage collection cycle should be performed at the function exit
that detects if there are any delegates that point to this function's stack
frame and throws an exception in this case.

P.S. I'd like this to be decided before 1.0 comes out.

-- 
AKhropov



More information about the Digitalmars-d mailing list