Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support conversion to AbstractArray #747

Merged
merged 4 commits into from
Mar 5, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/convert.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@
@inline convert(::Type{SA}, sa::SA) where {SA<:StaticArray} = sa
@inline convert(::Type{SA}, x::Tuple) where {SA<:StaticArray} = SA(x) # convert -> constructor. Hopefully no loops...

# support conversion to AbstractArray
AbstractArray{T}(sa::StaticArray{S,T}) where {S,T} = sa
AbstractArray{T,N}(sa::StaticArray{S,T,N}) where {S,T,N} = sa
AbstractArray{T}(sa::StaticArray{S,U}) where {S,T,U} = similar_type(typeof(sa),T,Size(sa))(Tuple(sa))
AbstractArray{T,N}(sa::StaticArray{S,U,N}) where {S,T,U,N} = similar_type(typeof(sa),T,Size(sa))(Tuple(sa))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think you need the Tuple here as the existing constructor on line 5 should take care of this?


# Constructing a Tuple from a StaticArray
@inline Tuple(a::StaticArray) = unroll_tuple(a, Length(a))

Expand Down
24 changes: 23 additions & 1 deletion test/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,29 @@ using StaticArrays, Test, LinearAlgebra
m = MVector(1, 2, 3)
@test @inferred(reverse(m))::typeof(m) == MVector(3, 2, 1)
end

@testset "Conversion to AbstractArray" begin
# Issue #746
# conversion to AbstractArray changes the eltype from Int to Float64
sv = SVector(1,2)
@test convert(AbstractArray{Float64}, sv) isa SVector{2,Float64}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be worth throwing in some @inferred here for some of these tests. For example:

Suggested change
@test convert(AbstractArray{Float64}, sv) isa SVector{2,Float64}
@test @inferred(convert(AbstractArray{Float64}, sv)) isa SVector{2,Float64}

@test convert(AbstractVector{Float64}, sv) isa SVector{2,Float64}
@test convert(AbstractArray{Float64}, sv) == sv
@test convert(AbstractArray{Int}, sv) === sv
sm = SMatrix{2,2}(1,2,3,4)
@test convert(AbstractArray{Float64,2}, sm) isa SMatrix{2,2,Float64}
@test convert(AbstractArray{Float64,2}, sm) == sm
@test convert(AbstractArray{Int,2}, sm) === sm
mv = MVector(1, 2, 3)
@test convert(AbstractArray{Float64}, mv) isa MVector{3,Float64}
@test convert(AbstractVector{Float64}, mv) isa MVector{3,Float64}
@test convert(AbstractArray{Float64}, mv) == mv
@test convert(AbstractArray{Int}, mv) === mv
mm = MMatrix{2, 2}(1, 2, 3, 4)
@test convert(AbstractArray{Float64,2}, mm) isa MMatrix{2,2,Float64}
@test convert(AbstractArray{Float64,2}, mm) == mm
@test convert(AbstractArray{Int,2}, mm) === mm
end
end

@testset "vcat() and hcat()" begin
Expand Down Expand Up @@ -191,4 +214,3 @@ end
@test @inferred(vcat(A, B)) === SMatrix{4, 2}([Matrix(A); Matrix(B)])
end
end