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 default constructors for AbstractRange #48894

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
7 changes: 7 additions & 0 deletions base/range.jl
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,11 @@ RangeStepStyle(::Type{<:AbstractRange{<:Integer}}) = RangeStepRegular()

convert(::Type{T}, r::AbstractRange) where {T<:AbstractRange} = r isa T ? r : T(r)::T

# there is no complex range
AbstractRange{T}(r::AbstractRange) where T<:Real = T(first(r)):T(step(r)):T(last(r))
AbstractArray{T,1}(r::AbstractRange) where T<:Real = AbstractRange{T}(r)
putianyi889 marked this conversation as resolved.
Show resolved Hide resolved
AbstractArray{T}(r::AbstractRange) where T<:Real = AbstractRange{T}(r)

## ordinal ranges

"""
Expand All @@ -287,6 +292,8 @@ Supertype for ranges with a step size of [`oneunit(T)`](@ref) with elements of t
"""
abstract type AbstractUnitRange{T} <: OrdinalRange{T,T} end

AbstractRange{T}(r::AbstractUnitRange) where {T<:Integer} = AbstractUnitRange{T}(r)

"""
StepRange{T, S} <: OrdinalRange{T, S}

Expand Down
3 changes: 3 additions & 0 deletions test/ranges.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1183,6 +1183,9 @@ end
@test convert(LinRange, 0.0:0.1:0.3) === LinRange{Float64}(0.0, 0.3, 4)
@test convert(LinRange, 0:3) === LinRange{Int}(0, 3, 4)

@test convert(AbstractRange{Int8},0:5) === convert(AbstractVector{Int8},0:5) ===convert(AbstractArray{Int8},0:5) === Int8(0):Int8(5)
putianyi889 marked this conversation as resolved.
Show resolved Hide resolved
@test convert(AbstractRange{Float64},0:5) === convert(AbstractVector{Float64},0:5) === convert(AbstractArray{Float64},0:5) === 0.:1.:5.

@test promote('a':'z', 1:2) === ('a':'z', 1:1:2)
@test eltype(['a':'z', 1:2]) == (StepRange{T,Int} where T)
end
Expand Down