Why 'in' works only for assoc arrays?

Andy Valencia dont at spam.me
Fri Dec 27 19:55:41 UTC 2024


On Friday, 27 December 2024 at 19:17:13 UTC, JN wrote:
> Why not make 'in' work for arrays (and strings also)?
>
> ```
> int[string] phonebook;
> if ("John" in phonebook) // works
>
> int[] numbers;
> if (3 in numbers) // doesn't work, compiler recommends 
> std.algorithm.find
>
> string buildLog;
> if ("build error" in buildLog) // doesn't work, compiler 
> recommends std.algorithm.find
> ```


One thing I learned which is somewhat topical--"x in y" is not a 
boolean expression; its value of the expression is a pointer to 
the value in the AA.  This would break for "string in string" 
searches, but seems OK for regular arrays.

Andy



More information about the Digitalmars-d-learn mailing list