Skip to content

setindex! with AbstractVector should work with iterable objects #42224

@ronisbr

Description

@ronisbr

Hi!

I have a class Quaternion in ReferenceFrameRotations.jl that was <:AbstractVector (4 elements). Hence, the following code used to work perfectly:

julia> v[4:7] = q

However, I had many problems in Zygote because my class was an array and the multiplication of two quaternions (2 4x1 vectors) provides another quaternion (4x1 vector). This behavior was breaking a lot of things.

Thus, I remove the super type <:AbstractVector and defined all the things necessary to make Quaternion and iterable object. In this case, things like:

julia> v[4:7] .= q

works. But

julia> v[4:7] = q

does not.

I am wondering if this is a bug or not. Notice that if RHS is iterable, maybe Julia has all the information to see if it can be assigned to LHS. My current workaround is to define:

@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

(q[:] converts a Quaternion into a Vector).

However, I am not sure if I am doing type piracy here.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions