Skip to content

Commit fedbc97

Browse files
committed
Add unary promote_op() for Bool
Fixes a small inconsistency between broadcast(+, ::Bool) and +(::Bool). Also useful to implement comparison operators for Nullables.
1 parent 858371f commit fedbc97

File tree

2 files changed

+7
-0
lines changed

2 files changed

+7
-0
lines changed

base/bool.jl

+2
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ cld(x::Bool, y::Bool) = div(x,y)
6060
rem(x::Bool, y::Bool) = y ? false : throw(DivideError())
6161
mod(x::Bool, y::Bool) = rem(x,y)
6262

63+
promote_op{T}(op::Type{T}, ::Type{Bool}) = T # to fix ambiguity
64+
promote_op(op, ::Type{Bool}) = typeof(op(true))
6365
promote_op(op, ::Type{Bool}, ::Type{Bool}) = typeof(op(true, true))
6466
promote_op(::typeof(^), ::Type{Bool}, ::Type{Bool}) = Bool
6567
promote_op{T<:Integer}(::typeof(^), ::Type{Bool}, ::Type{T}) = Bool

test/broadcast.jl

+5
Original file line numberDiff line numberDiff line change
@@ -196,3 +196,8 @@ end
196196
let a = broadcast(Float32, [3, 4, 5])
197197
@test eltype(a) == Float32
198198
end
199+
200+
# PR 16988
201+
@test Base.promote_op(+, Bool) === Int
202+
@test isa(broadcast(+, true), Array{Int,0})
203+
@test Base.promote_op(Float64, Bool) === Float64

0 commit comments

Comments
 (0)