diff --git a/base/broadcast.jl b/base/broadcast.jl index d087344eb5003f..dc4b33149a1f90 100644 --- a/base/broadcast.jl +++ b/base/broadcast.jl @@ -443,25 +443,20 @@ end ############################################################ # x[...] .= f.(y...) ---> broadcast!(f, dotview(x, ...), y...). -# The dotview function defaults to view, but we override it in +# The dotview function defaults to getindex, but we override it in # a few cases to get the expected in-place behavior without affecting # explicit calls to view. (All of this can go away if slices # are changed to generate views by default.) -dotview(args...) = view(args...) +dotview(args...) = getindex(args...) +dotview(A::AbstractArray, args...) = view(A, args...) # avoid splatting penalty in common cases: for nargs = 0:5 args = Symbol[Symbol("x",i) for i = 1:nargs] - eval(Expr(:(=), Expr(:call, :dotview, args...), Expr(:call, :view, args...))) + eval(Expr(:(=), Expr(:call, :dotview, args...), + Expr(:call, :getindex, args...))) + eval(Expr(:(=), Expr(:call, :dotview, :(A::AbstractArray), args...), + Expr(:call, :view, A, args...))) end -# for a[i...] .= ... where a is an array-of-arrays, just pass a[i...] directly -# to broadcast! -dotview{T<:AbstractArray,N,I<:Integer}(a::AbstractArray{T,N}, i::Vararg{I,N}) = - a[i...] - -# dict[k] .= ... should work if dict[k] is an array -dotview(a::Associative, k) = a[k] -dotview(a::Associative, k1, k2, ks...) = a[tuple(k1,k2,ks...)] - end # module diff --git a/doc/manual/functions.rst b/doc/manual/functions.rst index 6f43aec2fecad7..b950a3a942b670 100644 --- a/doc/manual/functions.rst +++ b/doc/manual/functions.rst @@ -660,7 +660,7 @@ calls do not allocate new arrays over and over again for the results except that, as above, the ``broadcast!`` loop is fused with any nested "dot" calls. For example, ``X .= sin.(Y)`` is equivalent to ``broadcast!(sin, X, Y)``, overwriting ``X`` with ``sin.(Y)`` in-place. -If the left-hand side is a ``getindex`` expression, e.g. +If the left-hand side is an array-indexing expression, e.g. ``X[2:end] .= sin.(Y)``, then it translates to ``broadcast!`` on a ``view``, e.g. ``broadcast!(sin, view(X, 2:endof(X)), Y)``, so that the left-hand side is updated in-place.