On Friday, 9 January 2015 at 06:18:53 UTC, Jonathan M Davis via
Digitalmars-d-learn wrote:
On Friday, January 09, 2015 00:20:07 Foo via
Digitalmars-d-learn wrote:
On Thursday, 8 January 2015 at 23:06:39 UTC, Nordlöw wrote:
> On Thursday, 8 January 2015 at 16:11:07 UTC, ketmar via
> Digitalmars-d-learn wrote:
>> how can it? compiler doesn't know what the code is supposed
>> to
>> do. if
>> compilers will know such things someday, we can stop writing
>> programs
>> altogether, as compilers will be able to write any program
>> for
>> us. ;-)
>
> Correction:
>
> I thought it would be nice if the compiler explained to me
> that
>
> key in aa ? aa[key]
>
> is a sub-optimal performance-wise.
You know, that you kan reuse the result of the in operator by
AAs?
example:
auto ptr = key in aa;
ptr ? *ptr : ValueType.init
This idiom is quite common:
if(auto ptrToValue = key in aa)
{
}
though I'm not sure that that quite fits in with what Nordlow
seems to be
trying to do with init. aa.get probably does a better job of
that, though
looking at the implementation for get, it's basically doing
what you're
suggesting:
auto p = key in aa;
return p ? *p : defaultValue;
though that has the downside of using a lazy parameter for the
default
value, which is convenient but doesn't do great things for
performance.
- Jonathan M Davis
I just wasn't sure that he knows about it.