# Error: Assigning a set to multiple positions of a vector of sets

**URL:** <https://discourse.julialang.org/t/error-assigning-a-set-to-multiple-positions-of-a-vector-of-sets/73394>\
**Category:** New to Julia\
**Created:** [December 20, 2021, 7:18pm UTC](https://discourse.julialang.org/t/error-assigning-a-set-to-multiple-positions-of-a-vector-of-sets/73394 "2021-12-20T19:18:12Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![bast](https://avatars.discourse-cdn.com/v4/letter/b/3e96dc/32.png) [@bast](https://discourse.julialang.org/u/bast)\
**Post date:** [December 20, 2021, 7:18pm UTC](https://discourse.julialang.org/t/error-assigning-a-set-to-multiple-positions-of-a-vector-of-sets/73394/1 "2021-12-20T19:18:12Z")

</div>

Hi,  
I recently started working with Julia and I ran into an error that I do not understand.  
I have a vector of sets of which one set contains one element. I now have candidate elements that I would like to add to the empty sets in the vector but only those elements that are not contained in any other sets should be added.

```julia
setlist = [Set() for _ = 1:3]

setlist[1] = Set('a')
potential_add = Set(['a', 'b'])

## This does not work
setlist[[2, 3]] .= setdiff!(potential_add, setlist...)

## For one position it works
setlist[2] = setdiff!(potential_add, setlist...)

## This produces the desired result 
union!.(setlist[[2, 3]], setdiff!(potential_add, setlist...))

```

Trying to assign the value to multiple positions at once yields the following error

```julia
ERROR: LoadError: MethodError: Cannot `convert` an object of type Char to an object of type Set{Any}
Closest candidates are:
  convert(::Type{T}, ::T) where T<:AbstractSet at set.jl:422  
  convert(::Type{T}, ::AbstractSet) where T<:AbstractSet at set.jl:423
  convert(::Type{T}, ::T) where T at essentials.jl:205        
  ...
Stacktrace:
 [1] setindex!(A::Vector{Set{Any}}, x::Char, i1::Int64)       
   @ Base .\array.jl:839
 [2] setindex!
   @ .\subarray.jl:317 [inlined]
 [3] macro expansion
   @ .\broadcast.jl:984 [inlined]
 [4] macro expansion
   @ .\simdloop.jl:77 [inlined]
 [5] copyto!
   @ .\broadcast.jl:983 [inlined]
 [6] copyto!
   @ .\broadcast.jl:936 [inlined]
 [7] materialize!
   @ .\broadcast.jl:894 [inlined]
 [8] materialize!(dest::SubArray{Set{Any}, 1, Vector{Set{Any}}, Tuple{Vector{Int64}}, false}, bc::Base.Broadcast.Broadcasted{Base.Broadcast.DefaultArrayStyle{1}, Nothing, typeof(identity), Tuple{Vector{Char}}})
   @ Base.Broadcast .\broadcast.jl:891
 [9] top-level scope

```

Desired result

```julia
3-element Vector{Set{Any}}:
 Set(['a'])
 Set(['b'])
 Set(['b'])

```

Why is it not possible to assign the set to multiple positions in the vector? Thank you for your thoughts!

---

<div class="post-metadata">

**Author:** ![bast](https://avatars.discourse-cdn.com/v4/letter/b/3e96dc/32.png) [@bast](https://discourse.julialang.org/u/bast)\
**Post date:** [December 20, 2021, 7:36pm UTC](https://discourse.julialang.org/t/error-assigning-a-set-to-multiple-positions-of-a-vector-of-sets/73394/2 "2021-12-20T19:36:34Z")

</div>

On closer inspection I noticed that the solution with the union only works with sets of size one.

---

<div class="post-metadata">

**Author:** ![Lilith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lilith/32/27492_2.png) [@Lilith](https://discourse.julialang.org/u/Lilith)\
**Post date:** [December 20, 2021, 8:08pm UTC](https://discourse.julialang.org/t/error-assigning-a-set-to-multiple-positions-of-a-vector-of-sets/73394/3 "2021-12-20T20:08:02Z")

</div>

When we write

```julia
a .= b

```

we invoke [broadcasting](https://docs.julialang.org/en/v1/manual/arrays/#Broadcasting). If `b` is a scaler (e.g. `1` or `'c'`) then index of `a` is set to `b`. If `b` is itself a collection, then each element of `a` is set to the corresponding element of `b` (e.g. `a .= [1,2,3]`). In your example, `b` is a `Set`, so it is trying to set each element of `setlist` to the corresponding element of your `setdiff!`. To avoid this, use

```julia
setlist[[2, 3]] .= [setdiff!(potential_add, setlist...)]

```

However! this may not be what you are looking for because this will make `setlist[2]` and `setlist[3]` be the same reference in memory, so a subsequent call to `push!(setlist[2], 'd')` will also impact `setlist[3]`.

The `union!` operation avoids this reference unification, but also uses broadcasting and so should be written

```julia
union!.(setlist[[2, 3]], [setdiff!(potential_add, setlist...)])

```

---

<div class="post-metadata">

**Author:** ![bast](https://avatars.discourse-cdn.com/v4/letter/b/3e96dc/32.png) [@bast](https://discourse.julialang.org/u/bast)\
**Post date:** [December 20, 2021, 10:25pm UTC](https://discourse.julialang.org/t/error-assigning-a-set-to-multiple-positions-of-a-vector-of-sets/73394/4 "2021-12-20T22:25:47Z")

</div>

Thank you very much for this quick and detailed response!

Very interesting point regarding the reference to memory, that would have potentially caused me a headache later on - thank you for pointing that out!
