You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
in order to check if a number X is within this range, one needs to remember that start of the range is inclusive and end is exclusive. Also, the end value can be greater than start. It would be helpful if Contains(Index) is added either as a first-class or an extension method in Range class:
The intended usage is to do a bound check using Range type: (lowerBound..upperBound).Contains(myValue).
In this case, the index from end (^) operator would use the upper bound (End) of Range object:
This is not how range and index work.
To be meaningful, range.Contains(index) should means collection[range] contains collection[index]. The result is relative to collection.Length.
This is not how range and index work.
To be meaningful, range.Contains(index) should means collection[range] contains collection[index]. The result is relative to collection.Length.
Right. That's why I was asking how you expect such an API to be used. I'd expect the intended usage to be in the implementation of other types, and in such types it is likely going to be wrong.
This proposal is for "is the given index within the range object", and not for "is the given index within the collection object". I understand that thus far, Range is mainly perceived for indexers and slice related use cases. However, Range (as an independent) type can have useful utility methods for more general use cases.
how you expect such an API to be used
I am using it to validate values read in web API:
varindex=ReadIndex();varrange=ReadRange();// validateif(!RangeContains(range,index)){returnError("index was out of range");}
..
Thanks. I think that should not be part of Range. It's arguably changing the expected meaning of Index, and I expect it's likely to do more harm (from being misused) than good.
Reacted by Kevin Jones, Akash K.C., Eric StJohn and Levi Broderick
Activity
stephentoub commentedon Dec 1, 2019
How would an API like that work for something like:
where it doesn't have enough information to produce an answer? Where/how would you use this method?
deadpaukevich commentedon Dec 1, 2019
am11 commentedon Dec 1, 2019
The intended usage is to do a bound check using
Rangetype:(lowerBound..upperBound).Contains(myValue).In this case, the
index from end (^)operator would use the upper bound (End) of Range object:huoyaoyuan commentedon Dec 1, 2019
This is not how range and index work.
To be meaningful,
range.Contains(index)should meanscollection[range]containscollection[index]. The result is relative tocollection.Length.stephentoub commentedon Dec 1, 2019
Right. That's why I was asking how you expect such an API to be used. I'd expect the intended usage to be in the implementation of other types, and in such types it is likely going to be wrong.
am11 commentedon Dec 1, 2019
This proposal is for "is the given index within the range object", and not for "is the given index within the collection object". I understand that thus far,
Rangeis mainly perceived for indexers and slice related use cases. However,Range(as an independent) type can have useful utility methods for more general use cases.I am using it to validate values read in web API:
stephentoub commentedon Dec 1, 2019
Thanks. I think that should not be part of Range. It's arguably changing the expected meaning of Index, and I expect it's likely to do more harm (from being misused) than good.