Skip to content

Commit

Permalink
Allow showing of StepRangLen{T} with generic T (#49516)
Browse files Browse the repository at this point in the history
  • Loading branch information
SamuelBadr authored May 5, 2023
1 parent ad939df commit ee95843
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
2 changes: 1 addition & 1 deletion base/range.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1101,7 +1101,7 @@ show(io::IO, r::AbstractRange) = print(io, repr(first(r)), ':', repr(step(r)), '
show(io::IO, r::UnitRange) = print(io, repr(first(r)), ':', repr(last(r)))
show(io::IO, r::OneTo) = print(io, "Base.OneTo(", r.stop, ")")
function show(io::IO, r::StepRangeLen)
if step(r) != 0
if !iszero(step(r))
print(io, repr(first(r)), ':', repr(step(r)), ':', repr(last(r)))
else
# ugly temporary printing, to avoid 0:0:0 etc.
Expand Down
13 changes: 13 additions & 0 deletions test/ranges.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2441,6 +2441,19 @@ end
@test test_firstindex(StepRange{Union{Int64,Int128},Int}(Int64(1), 1, Int128(0)))
end

@testset "PR 49516" begin
struct PR49516 <: Signed
n::Int
end
PR49516(f::PR49516) = f
Base.:*(x::Integer, f::PR49516) = PR49516(*(x, f.n))
Base.:+(f1::PR49516, f2::PR49516) = PR49516(+(f1.n, f2.n))
Base.show(io::IO, f::PR49516) = print(io, "PR49516(", f.n, ")")

srl = StepRangeLen(PR49516(1), PR49516(2), 10)
@test sprint(show, srl) == "PR49516(1):PR49516(2):PR49516(19)"
end

@testset "Inline StepRange Construction #49270" begin
x = rand(Float32, 80)
a = rand(round(Int, length(x) / 2):length(x), 10^6)
Expand Down

0 comments on commit ee95843

Please sign in to comment.