"heap" syntax
Russ Lewis
spamhole-2001-07-16 at deming-os.org
Wed Aug 30 09:45:13 PDT 2006
I mentioned this idea in the "future of lambda delegates" thread, but
don't know if anybody noticed. I'm starting a new thread for discussion.
The problem at hand was how to declare that a stack frame should reside
on the heap instead of on the stack. Somebody mentioned that we could
put *some* of the variables on the heap, which lead me to this idea:
int delegate() foo() {
int x,y,z; // these varaibles are on the stack
heap { // all variables declared inside this block are on the heap!
int a = x+y+z;
return delegate int() { return a++; }
// this is ok to return, since its 'this' pointer points to
// the heap, not the stack.
}
}
Implementation-wise, my thought is that the language automatically
allocates a region on the stack large enough for any variables in the
heap{} block. The stack frame then has a pointer to that value.
Deletate literals
* When a delegate literal *only* references variables inside a heap{}
block, then its 'this' pointer should point to the heap, of course.
* When a delegate literal accesses variables on the stack, then its
'this' pointer should point to the stack.
Q: Should it be legal to have a delegate which accesses both heap and
stack variables? Probably, but won't it sometimes make it non-obvious
which thing the delegate points to?
BTW: There is no reason that heap{} blocks couldn't be nested. There
might be cases where the outer heap area might become garbage while the
inner heap area does not.
More information about the Digitalmars-d
mailing list