[D2] How to not full closure?

Mike vertex at gmx.at
Mon Jul 14 12:43:49 PDT 2008


On Mon, 14 Jul 2008 21:31:47 +0200, Jarrett Billingsley  
<kb3ctd2 at yahoo.com> wrote:

> You have a point there.  And I think you may have something with using
> 'scope' to indicate nested functions whose stack frames are not to be
> heap-allocated.  It goes along with scope classes nicely.  You wouldn't  
> be
> able to return a scope delegate, and it makes sense -- "this delegate  
> only
> works in this scope.  When the scope leaves, the delegate is no longer
> valid."
>
> Adding "scope" also makes it possible for the compiler to check that you
> don't do stupid things statically, at least in many cases.

I agree, and bearophile's point is a good one, too (meaning D should do  
the safe thing by default).

I like the idea of new, though, because it indicates that new memory is  
being allocated on the heap. So I'd like this:

int b = 4;
auto heap = new { return b; }
auto stack = scope { return b; }
b++;
assert(heap() == 4);
assert(stack() == 5);

And "new" being the default it's legal to omit it:

auto heap = { ... }
auto stack = scope { ... }

-Mike

-- 
Using Opera's revolutionary e-mail client: http://www.opera.com/mail/



More information about the Digitalmars-d mailing list