Opt-out closures

Torarin torarind at gmail.com
Sun Jan 23 11:34:18 PST 2011


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


More information about the Digitalmars-d mailing list