Why 'in' works only for assoc arrays?

Sergey kornburn at yandex.ru
Sat Dec 28 09:52:02 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
> ```
>
> Good thing that compiler recommends what to use instead, but 
> why not just make it work for any container?

join the dark side of second best language :)

```python
a = [1,2,3]
d = dict(zip(a, ['a','b','c']))

if 3 in a:
     print("found in array")
if 3 in d:
     print("found in keys")
if 'a' in d.values():
     print("found in values")
```


More information about the Digitalmars-d-learn mailing list