"heap" syntax

Russ Lewis spamhole-2001-07-16 at deming-os.org
Wed Aug 30 14:34:34 PDT 2006


Johan Granberg wrote:
> I don't know what's the better syntax but i feel that any syntax that 
> require the variables to bee declared with special storage is prone to 
> mistakes and bugs.

I hear you, but disagree.  The problems with automatic detection have 
already been discussed a lot in this newsgroup, including:

* You can't detect if the delegate will outlive the function that 
created it (the Halting Problem bites you, even if you have access to 
all of the source).

* It is sometimes ambiguous whether a given delegate wants a copy of the 
stack variables, or the original.  Consider this snippet:
   void foo() {
     for(int i=0; i<10; i++)
       register(delegate int() { return i; });
     exec();
   }
   int delegate()[] registrationList;
   void register(int delegate() dg) {
     registrationList ~= dg;
   }
   void exec() {
     foreach(int delegate() dg; registrationList)
       writefln("value of this one is: ",dg());
   }
Now, in the code above, is each delegate supposed to get its own copy of 
i?  Or do they share it?

I'm in favor of an explicit syntax because it explicitly makes clear 
what the programmer intended to happen.



More information about the Digitalmars-d mailing list