Skip to content

Conversion to AbstractArray not defined #746

Closed
@daanhb

Description

@daanhb

Conversion of static vectors and matrices to an AbstractVector or AbstractArray with a wider element type seems to fail with an error:

julia> using StaticArrays

julia> convert(AbstractVector{BigFloat}, SVector(1.0, 2.0))
ERROR: setindex!() with non-isbitstype eltype is not supported by StaticArrays. Consider using SizedArray.
...

Yet, a type-stable conversion is possible because:

julia> convert(SVector{2,BigFloat}, SVector(1.0, 2.0)) isa AbstractVector{BigFloat}
true

Conversion to an abstract vector or abstract array with the same element type succeeds (via a fallback in Base).

I use conversions like this elsewhere in generic code that updates the element type of a vector. Currently, I have to add special cases for static vectors.

Would this just be a matter of adding conversion routines that invoke the right static array constructor? E.g., though it should be more general than this:

julia> import Base.convert

julia> convert(::Type{AbstractArray{T}}, v::SVector{N,S}) where {N,S,T} = convert(SVector{N,T}, v)
convert (generic function with 205 methods)

julia> convert(::Type{AbstractVector{T}}, v::SVector{N,S}) where {N,S,T} = convert(SVector{N,T}, v)
convert (generic function with 206 methods)

julia> convert(AbstractVector{BigFloat}, SVector(1.0, 2.0))
2-element SArray{Tuple{2},BigFloat,1,2} with indices SOneTo(2):
 1.0
 2.0

julia> ans isa AbstractVector{BigFloat}
true

This is not as problematic as going in the other direction (converting from abstract vectors to static vectors).

Metadata

Metadata

Assignees

No one assigned

    Labels

    featurefeatures and feature requests

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions