Delegate Literals + Immutability, pure and closures

David Nadlinger see at klickverbot.at
Thu Sep 29 14:12:26 PDT 2011


On 9/29/11 11:03 PM, dsimcha wrote:
> == Quote from Steven Schveighoffer (schveiguy at yahoo.com)'s article
>> Note that there is a workaround for 1:
>> DT noclosure(DT)(scope DT d) if(is(DT == delegate))
>> {
>>     return d;
>> }
>
> I know, but this defeats the nice zero-boilerplate solution to getting type
> inference and immutability when you want to conditionally assign one of several
> values to a variable.

Does this actually work with DMD? Last time I tested (some weeks ago), 
this worked as intended with LDC, but DMD still allocated a heap copy of 
the stack frame.

I don't remember the details, and unfortunately, I didn't keep a copy of 
the test case, but it was something like »int foo(scope int delegate() 
dg) { return dg(); } int main() { int a = 42; return foo({ return a; }); }«.

David


More information about the Digitalmars-d mailing list