D - closure
- BERO (33/33) Feb 12 2004 language manual says:
- Walter (4/10) Feb 13 2004 not
- BERO (31/32) Feb 13 2004 not nesesally allways copy stack frame.
- Russ Lewis (3/21) Feb 13 2004 How about a syntax to copy the stack variables at the time you create
- Walter (3/24) Feb 13 2004 Possibly in the future.
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
"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 arenotvalid 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
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
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 arenotvalid 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
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 arenotvalid 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