Is there any reason why in can't work with dynamic arrays?
Jonathan M Davis
newsgroup.d at jmdavisprog.com
Wed Sep 18 07:21:50 UTC 2019
On Wednesday, September 18, 2019 1:08:23 AM MDT JN via Digitalmars-d wrote:
> On Tuesday, 17 September 2019 at 17:43:39 UTC, Brett wrote:
> > T x;
> > if (y in x) ...
> > It just checks to see if y is in between 0 and length. Clearly
> > y has to be an int and clearly the semantics are equivalent to
> > AA and so it all works out rather than having an arbitrary
> > special case.
> > This is helpful when one has generate algorithms that can work
> > with AA's or DA's that use in.
> Wow! Does really in work like that with arrays? This is terrible.
> I'd never expect in to check if index is in range. In every
> language in existence (that I know of) that has 'in', 'in' is a
> search for existence of value, whether a linear search is
> required or not.
No, in does not work that way with dynamic arrays. It doesn't work with them
at all. That's just what the OP wants it to do.
If in worked with arrays, it would definitely check for the existence of a
value in the array, because that's what it does with other types, but its
big-o complexity is supposed to be no worse than O(log n) (which is what it
costs to look up a value in a balanced, binary tree such as a red-black
tree) so that its complexity can be relied upon in generic code. However, to
look up a value in a dynamic array would cost O(n), so it's not supported.
- Jonathan M Davis
More information about the Digitalmars-d