diff --git a/src/PTYQoL.jl b/src/PTYQoL.jl index fdda307..593d6db 100644 --- a/src/PTYQoL.jl +++ b/src/PTYQoL.jl @@ -110,6 +110,15 @@ show(io::IO, f::ComposedFunction{<:Fix2}) = print(io, f.outer.f, '(', f.inner, ' import Base: mapreduce mapreduce(f, op) = f() +# https://github.com/JuliaLang/julia/pull/51475 +import Base: zero, TwicePrecision +function zero(r::StepRangeLen{T,R,S}) where {T,R,S} + StepRangeLen{T}(zero(r.ref), zero(r.step), length(r), r.offset) +end +zero(r::LinRange) = LinRange{eltype(r)}(zero(first(r)), zero(last(r)), length(r)) +zero(r::Union{UnitRange, StepRange}) = zero(StepRangeLen(r)) +zero(r::TwicePrecision) = zero(typeof(r)) + import Base: searchsorted, searchsortedfirst, searchsortedlast, Ordering, Forward, ord, keytype, midpoint, lt function searchsortedfirst(v, x, lo::T, hi::T, o::Ordering)::keytype(v) where {T<:Integer} @@ -174,7 +183,7 @@ end # https://github.com/JuliaLang/julia/pull/56433 import Base: (:), +, -, TwicePrecision, zero, IEEEFloat -zero(::TwicePrecision{T}) where {T} = TwicePrecision(zero(T)) +# zero(::TwicePrecision{T}) where {T} = TwicePrecision(zero(T)) # overwrites https://github.com/JuliaLang/julia/pull/51475 TwicePrecision(x::AbstractIrrational) = TwicePrecision{Float64}(x) for op in (:+, :-) for (prec, twiceprec) in ((Float16, Float32), (Float32, Float64)) diff --git a/test/runtests.jl b/test/runtests.jl index 050b564..a116b18 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -60,6 +60,34 @@ using Test @test length(1:π:-1) == 0 @test length(1:π:1) == 1 end + + @testset "https://github.com/JuliaLang/julia/pull/51475" begin + @testset "StepRangeLen" begin + for r in (StepRangeLen(im, 2im, 10), + StepRangeLen{Bool}(false, true, 2), + 1.0:2.0:5.0) + z = zero(r) + @test r + z == r + @test typeof(z) == typeof(r) + end + end + @testset "LinRange" begin + for r in (LinRange{Int}(2, 3, 2), + LinRange(2, 3, 4), + LinRange{Bool}(false, true, 2)) + z = zero(r) + @test r + z == r + @test typeof(z) == typeof(r) + end + end + @testset "UnitRange/StepRange" begin + for r in (1:4, UnitRange(1.0, 2.0), + 1:1:4, false:true:true) + z = zero(r) + @test r + z == r + end + end + end end @testset "Misc" begin