About the in expression, Why can't use with array.

Ali Çehreli acehreli at yahoo.com
Fri Oct 25 19:49:05 UTC 2019


On 10/25/2019 02:25 AM, Dennis wrote:
 > On Friday, 25 October 2019 at 05:17:35 UTC, Ali Çehreli wrote:
 >> - Big O is different
 >
 > No it isn't.

Agreed and I've just realized a benefit of the 'in' operator for arrays, 
which I haven't heard before. (I don't follow all discussions in 
detail.) 'in' can

- linear search on regular arrays

- binary search on SortedRanges

So, this could would better for the programmer. 'in' would pick the 
fastest option without ever being slower than what the programmer would 
do. (The programmer can call canFind() on a regular but otherwise sorted 
array.) However, the following gotcha is still possible:

   auto sorted = sort(arr);
   if (element in arr) { // Oops; actually meant 'element in sorted'
     // ...
   }

I'm still not completely sold on the whole idea though because it's not 
a clear win.

Do others see other advantages in other places like templates? For 
example, could templates really be written generically for arrays and 
associative arrays?

Ali




More information about the Digitalmars-d-learn mailing list