!in operator?

Leandro Lucarella llucax at gmail.com
Mon May 25 10:17:48 PDT 2009


Frits van Bommel, el 25 de mayo a las 12:37 me escribiste:
> 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 :).

He is not joking:
http://www.dsource.org/projects/ldc/changeset/1418%3Af5f8c21ce6ef

Thank you =)

-- 
Leandro Lucarella (luca) | Blog colectivo: http://www.mazziblog.com.ar/blog/
----------------------------------------------------------------------------
GPG Key: 5F5A8D05 (F8CD F9A7 BF00 5431 4145  104C 949E BFB6 5F5A 8D05)
----------------------------------------------------------------------------
Ladrón no es cualquiera, ladrón es quien usurpa el bien ajeno en
beneficio propio, si no, no.
	-- Ricardo Vaporeso



More information about the Digitalmars-d mailing list