Stream: helpdesk (published)

Topic: Why does `v[:] = x` behaves differently than `v[:] .= x`?


view this post on Zulip iago-lito (Jul 28 2025 at 09:11):

I don't understand the following behaviour:

struct MyVec
    v::Vector{Int}
end

Base.getindex(v::MyVec, i...) = getindex(v.v, i...)
Base.setindex!(v::MyVec, x, i...) = setindex!(v.v, x, i...)

v = MyVec([1, 2, 3]) #  [1, 2, 3] ok.
v[1:2] = [4, 5]      #  [4, 5, 3] ok.
v[1:2] .= [6, 7]     #  [4, 5, 3] still.. why?

Can someone explain why = mutates v.v and not .= here?

view this post on Zulip Jakob Nybo Nissen (Jul 28 2025 at 09:28):

Not sure exactly, but it's got something to do with how broadcasting is not implemented correctly for non-arrays.
Nonetheless, I believe this is a bug. I would make an issue

view this post on Zulip iago-lito (Jul 28 2025 at 10:04):

Thank you. Cross-referencing here: https://github.com/JuliaLang/julia/issues/59115.


Last updated: Aug 14 2025 at 04:51 UTC