[D2] How to not full closure?

BCS ao at pathlink.com
Mon Jul 14 11:01:47 PDT 2008


Reply to mike,

> On Mon, 14 Jul 2008 04:39:58 +0200, davidl <davidl at 126.com> wrote:
> 
>> it's better to introduce a new keyword, closure.
>> 
>> there
>> 
>> on heap:
>> closure t = {
>> // my closure
>> return 3;
>> }
>> on stack:
>> delegate t = {
>> retirm 3;
>> }
> Wouldn't it suffice to overload the new keyword?
> 
> heap:
> 
> delegate t = new { return 3; }
> 
> stack:
> 
> delegate t = { return 3; }
> 
> That way it could work with delegate literals too:
> 
> int b = 3;
> foo(new { return b; });
> -Mike
> 

I like it. a few questions:

Would it only work with literals? what about nested functions?

I have always thought that delegates should be able to be scope on anything 
you want:

struct Foo { int bar; }

Foo* f = new Foo;
f.bar = 0;

auto inc = f.new { this.bar++; }

inc();
assert (f.bar==1);


or even:

int i = 5;
auto times = i.new (int j){return this*j;}
i=6;
assert(times(i) == 30);





More information about the Digitalmars-d mailing list