Skip to content

Commit 1b228ff

Browse files
authored
eltype-preserving indexing into LinRange (#35267)
1 parent e1dc967 commit 1b228ff

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

base/range.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -727,12 +727,12 @@ function getindex(r::StepRangeLen{T}, s::OrdinalRange{<:Integer}) where {T}
727727
return StepRangeLen{T}(ref, r.step*step(s), length(s), offset)
728728
end
729729

730-
function getindex(r::LinRange, s::OrdinalRange{<:Integer})
730+
function getindex(r::LinRange{T}, s::OrdinalRange{<:Integer}) where {T}
731731
@_inline_meta
732732
@boundscheck checkbounds(r, s)
733733
vfirst = unsafe_getindex(r, first(s))
734734
vlast = unsafe_getindex(r, last(s))
735-
return LinRange(vfirst, vlast, length(s))
735+
return LinRange{T}(vfirst, vlast, length(s))
736736
end
737737

738738
show(io::IO, r::AbstractRange) = print(io, repr(first(r)), ':', repr(step(r)), ':', repr(last(r)))

test/ranges.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1587,3 +1587,9 @@ end
15871587
@test step(r) === 2
15881588
@test collect(r) == ['a','c','e','g']
15891589
end
1590+
1591+
@testset "Return type of indexing with ranges" begin
1592+
for T = (Base.OneTo{Int}, UnitRange{Int}, StepRange{Int,Int}, StepRangeLen{Int}, LinRange{Int})
1593+
@test eltype(T(1:5)) === eltype(T(1:5)[1:2])
1594+
end
1595+
end

0 commit comments

Comments
 (0)