Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Statistics.middle(Base.Slice(4:6)) fails #113

Open
jlapeyre opened this issue Jun 8, 2022 · 2 comments
Open

Statistics.middle(Base.Slice(4:6)) fails #113

jlapeyre opened this issue Jun 8, 2022 · 2 comments

Comments

@jlapeyre
Copy link

jlapeyre commented Jun 8, 2022

julia> Statistics.middle(Base.Slice(4:6))
ERROR: BoundsError: attempt to access 3-element Base.Slice{UnitRange{Int64}} with indices 4:6 at index [1]

This is the method called.

middle(a::AbstractRange) = middle(a[1], a[end])

It's not clear from AbstractRange whether the first index must be 1:

  AbstractRange{T}


  Supertype for ranges with elements of type T. UnitRange and other types are subtypes of this.

But, Base.Slice violates this assumption. Maybe middle(a::AbstractRange) = middle(first(a), a[end]) is correct.

@nalimilan
Copy link
Member

Good catch. Probably better use a[begin] rather than first(x) for consistency and so that an error is thrown for empty ranges.

@jlapeyre
Copy link
Author

jlapeyre commented Jun 8, 2022

I found this because of the recent iterations of the interfaces-are-not-defined discussions. I saw a[1], which is suspicious, and asked "what is an AbstractRange ? I still don't know the answer. But, descending through subtypes, I find that one of them violates the assumption on the index of the first element. But, that looks like an ad hoc decision. It doesn't tell me who, strictly speaking, is in violation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants