skinny delegates

kinke noone at nowhere.com
Fri Aug 3 17:34:47 UTC 2018


On Friday, 3 August 2018 at 16:46:53 UTC, Jonathan Marler wrote:
> Maybe you could provide an example or 2 to demonstrate why 
> these would be requirements...we may have 2 different ideas on 
> how this would be implemented.

auto foo(/*mutable*/ int x)
{
    return { return ++x; };
}

void main()
{
     auto dg = foo(42);
     auto dg_copy = dg;
     // with the optimization, dg_copy would have its own context
     // in the ptr field, based on the current state in dg (42)

     const r1 = dg();
     const r2 = dg_copy(); // would be 43 with optimization
     assert(r1 == 43 && r2 == 44);
}

> do you think it should always be on and the developer shouldn't 
> need to or care to opt out of it?

Yes, by enforcing it in the language. No knowledge about this 
optimization necessary, no extra syntax, no extra dependency.

> Also, what about the developers that want to guarantee that the 
> optimization is occuring?

If they do know about this optimization, they probably aren't 
noobs and IMO should be able to have a look at LLVM IR or 
assembly to check whether it is optimized.
The only reason for wanting to enforce it coming to my mind 
ad-hoc is GC-free code (-betterC, bare metal), where @nogc should 
do. But there are also GC-using delegates which could be 
optimized this way.


More information about the Digitalmars-d mailing list