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?

Because `in` on associative arrays is a fast operation (O(1)).

On an array, searching for a value is a linear operation.

And Ali is right, really, you are talking about keys vs. values. For example, this doesn't work:

```
int[string] phonebook;
if(5551212 in phonebook) // error
```

which is the equivalent operation you are looking for.

-Steve

Reply via email to