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

Add promote_rule for OneTo and AbstractUnitRange #53795

Merged
merged 2 commits into from
Apr 29, 2024

Conversation

jishnub
Copy link
Contributor

@jishnub jishnub commented Mar 20, 2024

We may assume that a OneTo behaves like a UnitRange on promotion. With this, custom AbstractUnitRange types don't need to define promotion rules with OneTo, and the following works out of the box:

julia> struct MyUnitRange{T} <: AbstractUnitRange{T}
               range::UnitRange{T}
       end

julia> Base.first(r::MyUnitRange) = first(r.range)

julia> Base.last(r::MyUnitRange) = last(r.range)

julia> Base.size(r::MyUnitRange) = size(r.range)

julia> Base.length(r::MyUnitRange) = length(r.range)

julia> Base.getindex(r::MyUnitRange, i::Int) = getindex(r.range, i)

julia> promote(MyUnitRange(3:4), Base.OneTo(4))
(3:4, 1:4)

There is some potential for ambiguity if a package defines promote_rule(::AbstractUnitRange, ::CustomUnitRange), in which case a working promote_rule(::OneTo, ::CustomUnitRange) becomes ambiguous. However, a similar problem exists for UnitRange as well, and I think the benefits outweigh the risks.

@jishnub jishnub added the ranges Everything AbstractRange label Mar 20, 2024
@jishnub jishnub requested a review from mcabbott March 20, 2024 18:26
@jishnub jishnub requested a review from N5N3 April 29, 2024 07:04
@jishnub jishnub merged commit 68da780 into master Apr 29, 2024
7 checks passed
@jishnub jishnub deleted the jishnub/onetopromoterule branch April 29, 2024 16:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ranges Everything AbstractRange
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants