Recursive lambda functions?

Timon Gehr timon.gehr at gmx.ch
Mon Dec 30 15:08:34 PST 2013


On 12/30/2013 11:50 PM, Meta wrote:
>>
>> enum factorial5=(function int(a)=>a==0?1:a*__traits(parent,{})(a-1))(5);
>>
>> (In D, you need to specify the return type for a recursive function
>> declaration. If one doesn't here, DMD crashes, which is a bug.
>> https://d.puremagic.com/issues/show_bug.cgi?id=11848)
>
> And of course I'm wrong about that as soon as I post. No idea why one
> works when the other doesn't...

AFAICT, none of the cases is supposed to work: 
https://d.puremagic.com/issues/show_bug.cgi?id=8307

Presumably it is the same bug that causes the segfault. The underlying 
cause might be a missing check for completion of return type inference. 
In the first case, this means that the return type of the function is 
null when it is encountered in the function body. When null is checked 
for compatibility with multiplication with an int, a null pointer 
dereference occurs. In the second case, the return type has been partly 
resolved to 'int' as the first return statement has been analyzed to 
completion before the function call is encountered.


More information about the Digitalmars-d-learn mailing list