www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Closures issues

reply "Andrei Khropov" <andkhropov nospam_mtu-net.ru> writes:
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: 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
Jul 22 2006
next sibling parent "Andrei Khropov" <andkhropov nospam_mtu-net.ru> writes:
Andrei Khropov wrote:

 
 Hello everybody.
 
 There's a reoccuring issue with closures, i.e. downwards FunArg problem

I'm sorry, upwards of course (http://en.wikipedia.org/wiki/Funarg_problem#Upwards_funarg_problem) -- AKhropov
Jul 22 2006
prev sibling next sibling parent reply Walter Bright <newshound digitalmars.com> writes:
Andrei Khropov wrote:
 P.S. I'd like this to be decided before 1.0 comes out.

I think it's a 2.0 issue. Adding this won't break 1.0 code, so I don't think it'll be a problem.
Jul 22 2006
parent "Andrei Khropov" <andkhropov nospam_mtu-net.ru> writes:
Walter Bright wrote:

 Andrei Khropov wrote:
 P.S. I'd like this to be decided before 1.0 comes out.

I think it's a 2.0 issue. Adding this won't break 1.0 code, so I don't think it'll be a problem.

Thanks for rapid response. Looking forward to see it in 2.0. But for now some warning/error when we try to return a delegate pointing to a local frame from a function won't hurt. -- AKhropov
Jul 22 2006
prev sibling parent "Andrei Khropov" <andkhropov nospam_mtu-net.ru> writes:
Andrei Khropov wrote:

 
 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: digitalmars.D/39525
 ).     This will actually enable truly functions as first-class objects and 
    higher-order functions
    (because we could return delegates from functions) 

I recently read that C# 2.0 does that: http://msdn.microsoft.com/msdnmag/issues/06/00/C20/default.aspx#S7 Better keep up with the competition... -- AKhropov
Jul 24 2006