Skip to content

Commit

Permalink
array ± scalar for AbstractArray
Browse files Browse the repository at this point in the history
  • Loading branch information
stevengj committed Jun 17, 2014
1 parent 19987f6 commit 3046190
Showing 1 changed file with 9 additions and 10 deletions.
19 changes: 9 additions & 10 deletions base/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -767,16 +767,15 @@ for f in (:.+, :.-, :.*, :./, :.\, :.%, :div, :mod, :rem, :&, :|, :$)
end
end

# convenient and familiar aliases for broadcasting operations
# of array ± scalar:
(+)(A::StridedArray{Bool},x::Bool) = A .+ x
(+)(x::Bool,A::StridedArray{Bool}) = x .+ A
(-)(A::StridedArray{Bool},x::Bool) = A .- x
(-)(x::Bool,A::StridedArray{Bool}) = x .- A
(+)(A::StridedArray,x::Number) = A .+ x
(+)(x::Number,A::StridedArray) = x .+ A
(-)(A::StridedArray,x::Number) = A .- x
(-)(x::Number,A::StridedArray) = x .- A
# familiar aliases for broadcasting operations of array ± scalar (#7226):
(+)(A::AbstractArray{Bool},x::Bool) = A .+ x
(+)(x::Bool,A::AbstractArray{Bool}) = x .+ A
(-)(A::AbstractArray{Bool},x::Bool) = A .- x
(-)(x::Bool,A::AbstractArray{Bool}) = x .- A
(+)(A::AbstractArray,x::Number) = A .+ x
(+)(x::Number,A::AbstractArray) = x .+ A
(-)(A::AbstractArray,x::Number) = A .- x
(-)(x::Number,A::AbstractArray) = x .- A

# functions that should give an Int result for Bool arrays
for f in (:.+, :.-)
Expand Down

4 comments on commit 3046190

@sjkelly
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@stevengj @twadleigh this made some ambiguities in ImmutableArrays. Ex:

Warning: New definition 
    -(Number,Matrix4x3{T}) at /home/steve/.julia/ImmutableArrays/src/generate_arrays.jl:313
is ambiguous with: 
    -(Bool,AbstractArray{Bool,N}) at array.jl:774.
To fix, define 
    -(Bool,Matrix4x3{Bool})
before the new definition.

@stevengj
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See #7294. ImmutableArrays should override .+, not +, for array+scalar operations.

@sjkelly
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the reference! I filed an issue.

@stevengj
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.