Skip to content

Commit

Permalink
Make broadcast!(f, A) populate A via independent f() calls rather tha…
Browse files Browse the repository at this point in the history
…n fill!(A, f()). (#19722)

(cherry picked from commit 50a8b96)
  • Loading branch information
Sacha0 authored and tkelman committed Mar 1, 2017
1 parent cea0818 commit 4efacb6
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 1 deletion.
2 changes: 1 addition & 1 deletion base/broadcast.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ broadcast(f, x::Number...) = f(x...)

# special cases for "X .= ..." (broadcast!) assignments
broadcast!(::typeof(identity), X::AbstractArray, x::Number) = fill!(X, x)
broadcast!(f, X::AbstractArray) = fill!(X, f())
broadcast!(f, X::AbstractArray) = (@inbounds for I in eachindex(X); X[I] = f(); end; X)
broadcast!(f, X::AbstractArray, x::Number...) = fill!(X, f(x...))
function broadcast!{T,S,N}(::typeof(identity), x::AbstractArray{T,N}, y::AbstractArray{S,N})
check_broadcast_shape(size(x), size(y))
Expand Down
3 changes: 3 additions & 0 deletions test/broadcast.jl
Original file line number Diff line number Diff line change
Expand Up @@ -320,3 +320,6 @@ end
let A17984 = []
@test isa(abs.(A17984), Array{Any,1})
end

# Check that broadcast!(f, A) populates A via independent calls to f (#12277, #19722).
@test let z = 1; A = broadcast!(() -> z += 1, zeros(2)); A[1] != A[2]; end

0 comments on commit 4efacb6

Please sign in to comment.