diff --git a/src/fillbroadcast.jl b/src/fillbroadcast.jl index 82ba41e2..dad05362 100644 --- a/src/fillbroadcast.jl +++ b/src/fillbroadcast.jl @@ -198,21 +198,28 @@ _copy_oftype(A::AbstractArray{T,N}, ::Type{S}) where {T,N,S} = convert(AbstractA _copy_oftype(A::AbstractRange{T}, ::Type{T}) where T = copy(A) _copy_oftype(A::AbstractRange{T}, ::Type{S}) where {T,S} = map(S, A) -for op in (:+, -) +for op in (:+, :-) @eval begin - function broadcasted(::DefaultArrayStyle{1}, ::typeof($op), a::AbstractVector{T}, b::ZerosVector{V}) where {T,V} + function broadcasted(::DefaultArrayStyle{1}, ::typeof($op), a::AbstractRange{T}, b::ZerosVector{V}) where {T,V} broadcast_shape(axes(a), axes(b)) == axes(a) || throw(ArgumentError("Cannot broadcast $a and $b. Convert $b to a Vector first.")) _copy_oftype(a, promote_type(T,V)) end + function broadcasted(::DefaultArrayStyle{1}, ::typeof($op), a::AbstractVector{T}, b::ZerosVector{V}) where {T,V} + broadcast_shape(axes(a), axes(b)) == axes(a) || throw(ArgumentError("Cannot broadcast $a and $b. Convert $b to a Vector first.")) + TT = promote_type(T,V) + broadcasted(TT∘$op, a) + end - broadcasted(::DefaultArrayStyle{1}, ::typeof($op), a::AbstractFill{T,1}, b::ZerosVector) where T = + broadcasted(::DefaultArrayStyle{1}, ::typeof($op), a::AbstractFillVector{T}, b::ZerosVector) where T = Base.invoke(broadcasted, Tuple{DefaultArrayStyle, typeof($op), AbstractFill, AbstractFill}, DefaultArrayStyle{1}(), $op, a, b) end end -function broadcasted(::DefaultArrayStyle{1}, ::typeof(+), a::ZerosVector{T}, b::AbstractVector{V}) where {T,V} - broadcast_shape(axes(a), axes(b)) - _copy_oftype(b, promote_type(T,V)) +function broadcasted(S::DefaultArrayStyle{1}, ::typeof(+), a::ZerosVector, b::AbstractRange) + broadcasted(S, +, b, a) +end +function broadcasted(S::DefaultArrayStyle{1}, ::typeof(+), a::ZerosVector, b::AbstractVector) + broadcasted(S, +, b, a) end broadcasted(::DefaultArrayStyle{1}, ::typeof(+), a::ZerosVector, b::AbstractFillVector) = diff --git a/test/runtests.jl b/test/runtests.jl index cfd4ff4d..5834365e 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -908,6 +908,15 @@ end @test Zeros(10) .- Zeros(1,9) ≡ Zeros(10,9) @test Ones(10) .- Zeros(1,9) ≡ Ones(10,9) @test Ones(10) .- Ones(1,9) ≡ Zeros(10,9) + + end + + @testset "issue #208" begin + u = rand(2); v = Zeros(2) + @test Broadcast.broadcasted(-, u, v) isa Broadcast.Broadcasted + @test Broadcast.broadcasted(+, u, v) isa Broadcast.Broadcasted + @test Broadcast.broadcasted(-, v, u) isa Broadcast.Broadcasted + @test Broadcast.broadcasted(+, v, u) isa Broadcast.Broadcasted end @testset "Zero .*" begin