Skip to content

similar(::StaticArray, T) when !isbitstype(T) should give a SizedArray #799

Closed
@marius311

Description

@marius311

The similar(::StaticArray, T) method returns an MArray even if called on an SArray, rightfully so since there would be no reason to call similar if you couldn't mutate the array later.

However, if !isbitstype(T), then the returned MArray is still un-mutable later because of

if isbitstype(T)
GC.@preserve v unsafe_store!(Base.unsafe_convert(Ptr{T}, pointer_from_objref(v)), convert(T, val), i)
else
# This one is unsafe (#27)
# unsafe_store!(Base.unsafe_convert(Ptr{Ptr{Nothing}}, pointer_from_objref(v.data)), pointer_from_objref(val), i)
error("setindex!() with non-isbitstype eltype is not supported by StaticArrays. Consider using SizedArray.")

So I think the natural thing would be for similar(::StaticArray, T) to return a SizedArray when !isbitstype(T), as basically suggested by that error message.

If people here agree, I can take a stab at a PR, but also I'm happy if someone else that's more familiar with the code-base does it (probably much faster than I can).

FWIW, this would fix FluxML/Zygote.jl#686 where this came up for me.

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