Opt-out closures hack

Sean Eskapp eatingstaples at gmail.com
Sun Jan 23 12:12:27 PST 2011


== Quote from Torarin (torarind at gmail.com)'s article
> 2011/1/23 Sean Eskapp <eatingstaples at gmail.com>:
> > I want to be able to access the enclosing scope, but NOT after the function has
> > exited; I should have the option of accessing the enclosing scope, but at the cost
> > of making my delegate not a closure.
> >
> Until we have a dedicated syntax for it, I think you can use this hack:
> import std.traits;
> auto scopeDelegate(D)(scope D d) if (isDelegate!D)
> {
>   return d;
> }
> int main()
> {
>    StructWithDtor s;
>    trustedFunction(scopeDelegate({s.a = 5;}); // No heap allocation
> }
> Torarin

Yup, works great with everything I've tried! Thanks.


More information about the Digitalmars-d mailing list