-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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 default constructors for AbstractRange #48894
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it necessary to add constructors for abstract types?
julia> methods(AbstractRange)
# 0 methods for type constructor
julia> supertype(AbstractRange)
AbstractVector (alias for AbstractArray{T, 1} where T)
julia> methods(AbstractVector)
# 0 methods for type constructor
julia> supertype(AbstractVector)
Any
As you can see from added tests, it does eltype conversion for general ranges. |
@putianyi889 This seems like a good idea. Is this ready from your side? |
confirm.
not sure how many. maybe add 2-3 segments to every conversion test? |
Co-authored-by: Sheehan Olver <solver@mac.com>
Could you add tests for other range types, such as |
AbstractRange{T}(r::AbstractRange) where T = T(first(r)):T(step(r)):T(last(r)) | ||
AbstractArray{T,1}(r::AbstractRange) where T = AbstractRange{T}(r) | ||
AbstractArray{T}(r::AbstractRange) where T = AbstractRange{T}(r) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here are some failures from this T(first(r)):T(step(r)):T(last(r))
construction:
julia> convert(AbstractArray{Float32}, 0 * range(1, 2, 10)) |> length
ERROR: ArgumentError: range step cannot be zero
julia> convert(AbstractArray{Float16}, range(1/43^2, 1, 43)) |> length
42
I assume that range(start=T(first(r)), stop=T(last(r)), length=length(r))
would avoid these.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for pointing that out!
range
would fix StepRangeLen
, but changes OrdinalRange
to StepRangeLen
as well. Anyway, either OrdinalRange
or StepRangeLen
needs a specialized method.
As for the general fallback, I would keep the colon syntax until there are other issues.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wait the priority surely is to get the right answer?
If necessary, there can be specialised paths which try also to preserve certain types, like UnitRange and StepRange and LinRange, when possible. But the default path should not rely on a:b:c
colon magic to keep away from factually wrong answers.
Related posts
convert(AbstractArray{T}, ::AbstractRange)
and variants #27138