Having "in" for arrays

Jonathan M Davis newsgroup.d at jmdavisprog.com
Wed Nov 22 08:53:00 UTC 2017


On Wednesday, November 22, 2017 08:03:50 Fra Mecca via Digitalmars-d-learn 
wrote:
> Why doesn't D have a in keyword for arrays?
>
> The docs explains that you can use in only for associative arrays
> but I don't see the reasons for such decision.
>
>
> Example code:
>
> void main()
> {
>   auto v = ["r", "i", "o"];
>   assert ("r" in v);
> }
>
>
> That currently fails:
> main.d(4): Error: incompatible types for (("r") in (v)): 'string'
> and 'string[]'

in is supposed to be at worst O(log n), whereas to do the operation for an
array would be O(n). If you want to search for a specific element in an
array than use std.algorithm.find or std.algorithm.canFind.

- Jonathan M Davis



More information about the Digitalmars-d-learn mailing list