Skip to content

Commit

Permalink
Do not promote to Any
Browse files Browse the repository at this point in the history
Before:

    julia> push!!([Init(+)], 0)
    2-element Array{Any,1}:
      Init(+)
     0

After:

    julia> push!!([Init(+)], 0)
    2-element Array{Union{InitialValues.InitialValueOf{typeof(+)}, Int64},1}:
      Init(+)
     0
  • Loading branch information
tkf committed May 15, 2020
1 parent 065cc1c commit 6ff8816
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/InitialValues.jl
Expand Up @@ -203,6 +203,8 @@ end
@disambiguate Base.min Missing
@disambiguate Base.max Missing

Base.promote_rule(::Type{I}, ::Type{S}) where {I<:InitialValue,S} = Union{I,S}

const ZeroType = Union{
SpecificInitialValue{typeof(+)},
SpecificInitialValue{typeof(Base.add_sum)},
Expand Down
5 changes: 5 additions & 0 deletions test/interop/test_bangbang.jl
Expand Up @@ -12,4 +12,9 @@ using InitialValues: Init, hasinitialvalue
@test append!!([1], Init(append!!)) == [1]
end

@testset "promote" begin
init = Init(+)
@test eltype(push!!([init], 0)) == Union{typeof(init),Int}
end

end # module
10 changes: 9 additions & 1 deletion test/test_basics.jl
Expand Up @@ -4,8 +4,9 @@ using Test
using InitialValues
using InitialValues: isknown, hasinitialvalue

OPS = [*, +, |, &, min, max, Base.add_sum, Base.mul_prod]

@testset for op in [*, +, |, &, min, max, Base.add_sum, Base.mul_prod]
@testset for op in OPS
@test op(Init(op), :anything) === :anything
@test hasinitialvalue(op)
@test hasinitialvalue(typeof(op))
Expand Down Expand Up @@ -39,6 +40,13 @@ end
@test max(Init(max), missing) === missing
end

@testset "promote" begin
for op in OPS
T = typeof(Init(op))
@test promote_type(T, Val{0}) == Union{T,Val{0}}
end
end

@testset "convert" begin
@testset "float" begin
@test float(Init(+)) === 0.0
Expand Down

0 comments on commit 6ff8816

Please sign in to comment.