Why don't lazy parameters bind to delegates? Was: Feature to get or add value to an associative array.
Steven Schveighoffer
schveiguy at yahoo.com
Fri Apr 20 23:27:20 UTC 2018
On 4/20/18 6:46 PM, Giles Bathgate wrote:
> On Friday, 20 April 2018 at 22:21:13 UTC, Jonathan M Davis wrote:
>> Honestly, I think that it's a terrible idea to special-case it like
>> that. If we want to argue for making it work in the language, that's
>> fine, but if we special-case it like this, then it will work with some
>> functions that have lazy parameters and not others, and the result
>> will be confusing. Besides, all it takes to be able to pass a lamdba
>> or delegate to a lazy parameter is to actually call it when passing
>> it. So, if you add parens after the braces, it works. There's no need
>> to go and add a special case for it to the function.
>
> Again lack of experience, so I presume you can just do:
>
> bool inserted = false;
> auto p = aa.getOrAdd("key", {inserted = true; return new Person; }());
>
> I hadn't realised that until now. I enjoy your brutal honesty by the way ;)
>
>
The drawback here, of course, is that it's a lambda calling a lambda (if
you end up using the value).
But of course, your overload was the same thing.
I'm just surprised it doesn't work, especially when this works:
// lazy variadic
void foo(int delegate()[] dgs...)
{
dgs[0]();
}
foo(1); // ok, same as { return 1; }
foo({inserted = true; return 1;}); // ok
Of course, it's not as nice syntax inside the function.
-Steve
More information about the Digitalmars-d
mailing list