# Help defining setindex! with a custom structure

**URL:** <https://discourse.julialang.org/t/help-defining-setindex-with-a-custom-structure/68055>\
**Category:** General Usage\
**Created:** [September 12, 2021, 3:27pm UTC](https://discourse.julialang.org/t/help-defining-setindex-with-a-custom-structure/68055 "2021-09-12T15:27:02Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ronis\_BR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ronis_br/32/50999_2.png) [@Ronis\_BR](https://discourse.julialang.org/u/Ronis_BR)\
**Post date:** [September 12, 2021, 3:27pm UTC](https://discourse.julialang.org/t/help-defining-setindex-with-a-custom-structure/68055/1 "2021-09-12T15:27:02Z")

</div>

Hi!

I have a struct called `Quaternion` (which has 4 elements) and I want to make the following code work:

```julia
julia> v = zeros(10);

julia> q = Quaternion(1.0, 2.0, 3.0, 4.0);

julia> v[4:7] = q

```

It fails with the following error:

```julia
ERROR: ArgumentError: indexed assignment with a single value to possibly many locations is not supported; perhaps use broadcasting `.=` instead?

```

The operation with `.=` works, but I need the first.

---

<div class="post-metadata">

**Author:** ![SteffenPL](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/steffenpl/32/206270_2.png) [@SteffenPL](https://discourse.julialang.org/u/SteffenPL)\
**Post date:** [September 12, 2021, 3:37pm UTC](https://discourse.julialang.org/t/help-defining-setindex-with-a-custom-structure/68055/2 "2021-09-12T15:37:03Z")

</div>

Here is a bad, but working implementation

```julia
struct Quaternion; x; y; z; w; end
function Base.setindex!(a::Vector{Float64}, v::Quaternion, I::UnitRange)
       a[I] .= [v.x,v.y,v.z,v.w]
end

v = zeros(10)
q = Quaternion(1.0,2.0,3.0,4.0)
v[4:7] = q

```

So, you need to define `setindex!` for these types (with probably a better implementation than mine).

---

<div class="post-metadata">

**Author:** ![Ronis\_BR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ronis_br/32/50999_2.png) [@Ronis\_BR](https://discourse.julialang.org/u/Ronis_BR)\
**Post date:** [September 12, 2021, 3:39pm UTC](https://discourse.julialang.org/t/help-defining-setindex-with-a-custom-structure/68055/3 "2021-09-12T15:39:33Z")

</div>

The problem of doing that is that I got **a lot** of problems due to ambiguous definition of `setindex!` from Base.

Shouldn’t Base support it out of the box if RHS is iterable?

---

<div class="post-metadata">

**Author:** ![SteffenPL](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/steffenpl/32/206270_2.png) [@SteffenPL](https://discourse.julialang.org/u/SteffenPL)\
**Post date:** [September 12, 2021, 3:42pm UTC](https://discourse.julialang.org/t/help-defining-setindex-with-a-custom-structure/68055/4 "2021-09-12T15:42:49Z")

</div>

Did you implement all the things from [Interfaces · The Julia Language](https://docs.julialang.org/en/v1/manual/interfaces/) ?  
(If yes and if it is easy: do you mind to share the code, than it’s easier to mess around with it.)

---

<div class="post-metadata">

**Author:** ![SteffenPL](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/steffenpl/32/206270_2.png) [@SteffenPL](https://discourse.julialang.org/u/SteffenPL)\
**Post date:** [September 12, 2021, 3:48pm UTC](https://discourse.julialang.org/t/help-defining-setindex-with-a-custom-structure/68055/5 "2021-09-12T15:48:01Z")

</div>

I guess according to these lines [https://github.com/JuliaLang/julia/blob/211ed19f618fbb8d37e13d59f90e3f2bc1fc9615/base/array.jl#L951](https://github.com/JuliaLang/julia/blob/211ed19f618fbb8d37e13d59f90e3f2bc1fc9615/base/array.jl#L951) is does the job only automatically if `Quaternion` is a instance of `AbstractArray`

---

<div class="post-metadata">

**Author:** ![Ronis\_BR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ronis_br/32/50999_2.png) [@Ronis\_BR](https://discourse.julialang.org/u/Ronis_BR)\
**Post date:** [September 12, 2021, 3:58pm UTC](https://discourse.julialang.org/t/help-defining-setindex-with-a-custom-structure/68055/6 "2021-09-12T15:58:19Z")

</div>

I will push the code and create a MWE. It is in ReferenceFrameRotations.jl.

`Quaternion` used to be `<:AbstractArray`, but I was getting a lot of problems in Zygote. In this case, we have an array in which the multiplication `q1 * q2` (2 4x1 vectors) was leading to another 4x1 array. This was causing a lot of problems. Hence, I am dropping the supertype now and trying to fix things.

---

<div class="post-metadata">

**Author:** ![SteffenPL](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/steffenpl/32/206270_2.png) [@SteffenPL](https://discourse.julialang.org/u/SteffenPL)\
**Post date:** [September 12, 2021, 4:09pm UTC](https://discourse.julialang.org/t/help-defining-setindex-with-a-custom-structure/68055/7 "2021-09-12T16:09:28Z")

</div>

Ah, I see. Given that `Quaternion` is not a subtype of anything, I guess Julia has no chance of figuring out at the point of `setindex!(...)` that your type could be iterable (since that would need to be encoded in the type either way)? I think I can’t help, it looks tricky. Maybe someone else has a good idea 🙂

---

<div class="post-metadata">

**Author:** ![Ronis\_BR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ronis_br/32/50999_2.png) [@Ronis\_BR](https://discourse.julialang.org/u/Ronis_BR)\
**Post date:** [September 12, 2021, 5:24pm UTC](https://discourse.julialang.org/t/help-defining-setindex-with-a-custom-structure/68055/8 "2021-09-12T17:24:58Z")

</div>

`Quaternion` is iterable, it has all the API functions defined. I think Julia could verify if RHS is iterable and has the right size, and then make everything work.

---

<div class="post-metadata">

**Author:** ![Ronis\_BR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ronis_br/32/50999_2.png) [@Ronis\_BR](https://discourse.julialang.org/u/Ronis_BR)\
**Post date:** [September 12, 2021, 5:48pm UTC](https://discourse.julialang.org/t/help-defining-setindex-with-a-custom-structure/68055/9 "2021-09-12T17:48:28Z")

</div>

I think I reached a good tradeoff by defining this function as you mentioned before:

```julia
@inline function setindex!(v::Vector{T}, q::Quaternion, I::UnitRange) where T
    # We can use all the funcion in static arrays.
    return setindex!(v, q[:], I)
end

```

My last attempt was using `v::AbstractVector`, and that’s why I was getting too many problems.

---

<div class="post-metadata">

**Author:** ![Ronis\_BR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ronis_br/32/50999_2.png) [@Ronis\_BR](https://discourse.julialang.org/u/Ronis_BR)\
**Post date:** [September 12, 2021, 5:57pm UTC](https://discourse.julialang.org/t/help-defining-setindex-with-a-custom-structure/68055/10 "2021-09-12T17:57:29Z")

</div>

Thanks for the help @SteffenPL ! I opened an issue to see what the devs say:

[https://github.com/JuliaLang/julia/issues/42224](https://github.com/JuliaLang/julia/issues/42224)

I really think this should work for iterable objects.
