In Expression for Static and DYnamic Arrays

Nick Sabalausky SeeWebsiteToContactMe at semitwist.com
Fri Oct 12 13:42:03 PDT 2012


On Fri, 12 Oct 2012 19:18:31 +0200
"Jonathan M Davis" <jmdavisProg at gmx.com> wrote:

> On Thursday, October 11, 2012 18:45:36 Rizo Isrof wrote:
> > Hi,
> > 
> > Why the `in` expression can only be applied to associative
> > arrays and cannot be used with static or dynamic arrays as
> > it is possible with, _e.g._, Python?
> > 
> > The following code is not legal:
> > ----
> > int[] a = [1,2,3,4,5];
> > if (1 in a) { }
> > ----
> > Are there any technical explanation for this limitation?
> 
> Because that would mean than in was O(n), whereas it's generally
> assumed to be at least o(log n) (which is what you'd get in a
> balanced binary tree such as red-black tree).

Another explanation is that 'in' searches the keys, ie indexes, not the
values. So making it search the values on an array would arguably be
inconsistent.

Fortunately, like Jonathan said, there's this which is pretty close:
if(a.canFind(1))



More information about the Digitalmars-d-learn mailing list