Update Vector with dot Operator - Different Behavior when using Vector and Other Types

julia> v1 = Any[2, 3, 5, 7, 11, 13, 17]
7-element Vector{Any}:
  2
  3
  5
  7
 11
 13
 17

julia> v1[1:3] .= Ref([4,5,6])
3-element view(::Vector{Any}, 1:3) with eltype Any:
 [4, 5, 6]
 [4, 5, 6]
 [4, 5, 6]

julia> v1
7-element Vector{Any}:
   [4, 5, 6]
   [4, 5, 6]
   [4, 5, 6]
  7
 11
 13
 17
julia> v1[1:3] .= "1"
3-element view(::Vector{Any}, 1:3) with eltype Any:
 "1"
 "1"
 "1"

julia> v1
7-element Vector{Any}:
   "1"
   "1"
   "1"
  7
 11
 13
 17

Taking your questions one at a time:

  1. v[1:3] .= [4,5,6] broadcasts over left and right hand side objects, unless told otherwise.
  2. If you want to not broadcast over the RHS, tell Julia not to do so by enclosing the vector in Ref.
  3. But then you need to set up the vector v1 so it can hold objects other than Int. Hence the Any in v1 = Any[2,3,5,7,11,13,17]. This is also why your string assignment fails (not because of broadcasting.
2 Likes