Skip to content

API Proposal: Range.Contains(Index) #417

Closed

Description

@am11

Given a range

[start..end]

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:

public class Range
{
    public bool Contains(Index subject) => Start.Value < End.Value ? 
        subject.Value >= Start.Value && subject.Value < End.Value :
        false;
}

Inspired by:

Activity

  1. stephentoub commented on Dec 1, 2019

    @stephentoub
    Member

    How would an API like that work for something like:

    [1..3].Contains(^1)
    

    where it doesn't have enough information to produce an answer? Where/how would you use this method?

  2. deadpaukevich commented on Dec 1, 2019

    @deadpaukevich
  3. am11 commented on Dec 1, 2019

    @am11
    MemberAuthor

    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:

    (1..3).Contains(^0) => false
    (1..3).Contains(^1) => true
    (1..3).Contains(^2) => true
    (1..3).Contains(^3) => false
    (1..3).Contains(^4) => false
  4. huoyaoyuan commented on Dec 1, 2019

    @huoyaoyuan
    Member

    (1..3).Contains(^0) => false
    (1..3).Contains(^1) => true
    (1..3).Contains(^2) => true
    (1..3).Contains(^3) => false
    (1..3).Contains(^4) => false

    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.

  5. stephentoub commented on Dec 1, 2019

    @stephentoub
    Member

    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.

  6. am11 commented on Dec 1, 2019

    @am11
    MemberAuthor

    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:

    var index = ReadIndex();
    var range = ReadRange();
    
    // validate
    if (!RangeContains(range, index))
    {
       return Error("index was out of range"); 
    }
    
    ..
  7. stephentoub commented on Dec 1, 2019

    @stephentoub
    Member

    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.

  8. ghost locked as resolved and limited conversation to collaborators on Dec 11, 2020
  9. removed
    untriagedNew issue has not been triaged by the area owner
    on Jun 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions