Stewart Gordon wrote:
Jason House wrote:
<snip>
Method 1:

if (x !in y)
  foo();
else{
  auto z = x in y;
  bar(z);
}

Method 2:

auto z = x in y;
if (z is null)
  foo;
else
  bar(z);

Method 1 essentially calls in twice while method 2 calls in once.
<snip>

But there's no requirement to look it up after finding out whether it's there or not.

And how's it any different from

if (x in y) {
    auto z = x in y;
    bar(z);
} else {
    foo();
}

or even

if (x in y) {
    bar(y[x]);
} else {
    foo();
}

?

Besides, why would any decent compiler not optimise it to a single lookup?

Interesting fact: LDC currently optimizes the first case (at -O3), but not the second. That's because it apparently uses a different libcall for 'x in y' than it does for 'y[x]' -- even though the code in the libcalls are equivalent.

I think I just found inspiration for my next commit :).

Reply via email to