www.digitalmars.com         C & C++   DMDScript  

D - closure

reply "BERO" <berobero users.sourceforge.net> writes:
language manual says:
"The stack variables, however, are not valid once the function declaring
them has exited, in the same manner that pointers to stack variables are not
valid upon exit from a function:"

so copy stack frame before exist funcion.
copied stack frame will be freed in GC.

-----
 alias int delegate(int) dg1;

 union dg2 {
  dg1 dg;
  uint bp;
 }


dg1 addx(int x)
{
 int add(int y) { return x+y;}

 dg2 dg;
 dg.dg = &add;

 uint sp;
 asm { mov sp[EBP],ESP; }

 uint* frame = ((uint*)sp)[0..(dg.bp - sp + 0/* arguments size */)/4].dup;
 dg.bp = cast(uint)frame + dg.bp - sp;

 return dg.dg;
}

int main()
{
 dg1 add1 = addx(1);
 dg1 add2 = addx(2);

 printf("1+1=%d\n",add1(1));
 printf("1+2=%d\n",add1(2));
 printf("2+1=%d\n",add2(1));
 printf("2+2=%d\n",add2(2));

 return 0;
}
Feb 12 2004
parent reply "Walter" <walter digitalmars.com> writes:
"BERO" <berobero users.sourceforge.net> wrote in message
news:c0h7ql$2nef$1 digitaldaemon.com...
 language manual says:
 "The stack variables, however, are not valid once the function declaring
 them has exited, in the same manner that pointers to stack variables are
not
 valid upon exit from a function:"

 so copy stack frame before exist funcion.
 copied stack frame will be freed in GC.
That is a workable solution, but I hated to give up the efficiency.
Feb 13 2004
next sibling parent "BERO" <berobero users.sourceforge.net> writes:
 That is a workable solution, but I hated to give up the efficiency.
not nesesally allways copy stack frame. I think both closure and delegte can be used properly. using "closure" syntax like "function" and "delegate" if supported, or user level (template) function such as "delegate2closure". in class based OO paradigm, class addx { public: this(int x_) { x = _x; } int opCall(int y) { return x+y; } private: int x; } addx add1 = new addx(1); int result = add1(1); I think class and closure efficiency is almost same (or closure is bit fast). closure is interesting as a language paradigm, but my imprementation is not practical. mkfunc(..) { char buf[N]; char *p = buf; func(...) { //to use *p is DANGEROUS //*p point old stack frame, not copied frame } delegate.. dg = &func; // stack frame copy return dg; } bero
Feb 13 2004
prev sibling parent reply Russ Lewis <spamhole-2001-07-16 deming-os.org> writes:
How about a syntax to copy the stack variables at the time you create 
the delegate?  That way we have both...

Walter wrote:
 "BERO" <berobero users.sourceforge.net> wrote in message
 news:c0h7ql$2nef$1 digitaldaemon.com...
 
language manual says:
"The stack variables, however, are not valid once the function declaring
them has exited, in the same manner that pointers to stack variables are
not
valid upon exit from a function:"

so copy stack frame before exist funcion.
copied stack frame will be freed in GC.
That is a workable solution, but I hated to give up the efficiency.
Feb 13 2004
parent "Walter" <walter digitalmars.com> writes:
Possibly in the future.

"Russ Lewis" <spamhole-2001-07-16 deming-os.org> wrote in message
news:c0jk5l$fuj$1 digitaldaemon.com...
 How about a syntax to copy the stack variables at the time you create
 the delegate?  That way we have both...

 Walter wrote:
 "BERO" <berobero users.sourceforge.net> wrote in message
 news:c0h7ql$2nef$1 digitaldaemon.com...

language manual says:
"The stack variables, however, are not valid once the function declaring
them has exited, in the same manner that pointers to stack variables are
not
valid upon exit from a function:"

so copy stack frame before exist funcion.
copied stack frame will be freed in GC.
That is a workable solution, but I hated to give up the efficiency.
Feb 13 2004