JuliaFolds / BangBang.jl

Immutables as mutables, mutables as immutables.
MIT License
108 stars 11 forks source link

Fix for mutating `setindex!!` with higher dimensional indices #238

Closed torfjelde closed 10 months ago

torfjelde commented 1 year ago

Currently, we have the following behavior:

julia> x = rand(2, 3);

julia> BangBang.setindex!!(x, ones(2), :, 1)
2×3 Matrix{Any}:
 1.0  0.96169   0.0951699
 1.0  0.799981  0.254469

i.e. we hit the immutable version. This is a bug in the current possible implementation for BangBang._setindex!, which only checks that the eltype is valid; in the above case, the eltype(x) is of course Float64 but the element provided x[:, 1] is of type Vector{Float64}, hence it fails.

In this PR, we have

julia> x = rand(2, 3);

julia> BangBang.setindex!!(x, ones(2), :, 1)
2×3 Matrix{Float64}:
 1.0  0.971977  0.761834
 1.0  0.294437  0.529877

as desired.

yebai commented 1 year ago

@DilumAluthge Would you be able to take a look at this bugfix PR or tag someone else who can approve it? This bug causes issues in Turing.