-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
make x[...] .= ... assign in-place #17546
Conversation
(@timholy, is there anything about the current |
(let* ((ex (partially-expand-ref expr)) | ||
(stmts (butlast (cdr ex))) | ||
(refex (last (cdr ex))) | ||
(nuref `(call view ,(caddr refex) ,@(cdddr refex)))) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@JeffBezanson, should this be (top view)
rather than view
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes; I would go with top
to avoid capturing locals that might be called view
or broadcast
.
Marking as 0.5, since this needs to be resolved one way or the other (either by merging this in some form or by reverting #17510) by 0.5. There is code in existing packages that uses |
Okay to merge? |
would be good to get a confirmation on #17546 (comment) ? |
@tkelman, even if there is some case where If |
Seems like sound reasoning, @stevengj – in favor of merging. |
One case that we can't handle yet is that of a dictionary. Neither One way to handle this would be to implement a |
I think people are going to be confused when julia> a = [[4, 5], [6, 7]]
julia> a[1] .= 3
ERROR: MethodError: Cannot `convert` an object of type Int64 to an object of type Array{Int64,1}
This may have arisen from a call to the constructor Array{Int64,1}(...),
since type constructors fall back to convert methods.
in fill!(::SubArray{Array{Int64,1},0,Array{Array{Int64,1},1},Tuple{Int64},false}, ::Int64) at ./multidimensional.jl:549
in broadcast!(::Base.#identity, ::SubArray{Array{Int64,1},0,Array{Array{Int64,1},1},Tuple{Int64},false}, ::Int64) at ./broadcast.jl:19
in eval(::Module, ::Any) at ./boot.jl:234
in macro expansion at ./REPL.jl:92 [inlined]
in (::Base.REPL.##3#4{Base.REPL.REPLBackend})() at ./event.jl:46 The way to get the intended result is: julia> a[1][:] .= 3
2-element SubArray{Int64,1,Array{Int64,1},Tuple{Colon},true}:
3
3
julia> a
2-element Array{Array{Int64,1},1}:
[3,3]
[6,7] but that's not exactly obvious and might require a manual entry (unless a special case is made for scalar indexing). AFAIK this is why MATLAB cell arrays have two types of indexing brackets: |
@iamed2, I think that most cases of interest could be supported by adding appropriate methods to e.g. your particular case could be handled by defining: Base.broadcast!{T<:AbstractArray}(f, x::SubArray{T,0}, args...) = broadcast!(f, x[], args...)
Base.broadcast!{T<:AbstractArray}(f::typeof(identity), x::SubArray{T,0}, arg::Number) = broadcast!(f, x[], arg) # prevent method ambiguity We could also get more fine-grained control by defining a |
Note also that this PR can be reverted in 0.6 if the arraypocolypse happens and slices become views. So potentially this is just a temporary fix. |
…d dictionaries of arrays
* make x[...] .= ... assign in-place (fixes bug in JuliaLang#17510) * doc fix * You're the top! You're the Coliseum. * better var name
…d dictionaries of arrays
This fixes a bug in #17510:
x[...] .= ...
andx[...] .+= ...
etcetera should callbroadcast!
onview(x, ...)
, so that it continues to work in-place. (In Julia 0.4,x[...] .+= ...
translates into asetindex!
call.)