Just for info and in case this interests someone, I recently wrote this in some of my code:
struct HeterogeneousStaticArray{S<:Tuple,D,T,X<:Tuple} <: StaticArray{S,T,D}
data::X
end
# HeterogeneousStaticArray{S,D,T}(x::X) where{S,D,T,X} =
# HeterogeneousStaticArray{S,D,T,X}(x)
const HSVector{N,T} =
HeterogeneousStaticArray{Tuple{N},1,T}
const HSMatrix{N1,N2,T} =
HeterogeneousStaticArray{Tuple{N1,N2},2,T}
@propagate_inbounds Base.getindex(a::HeterogeneousStaticArray, i::Int) = a.data[i]
# these two lines are copied from LinearAlgebra/adjtrans.jl,
# only removing the typeassert for the result:
@propagate_inbounds Base.getindex(v::LinearAlgebra.AdjOrTrans{T,<:HSVector},
i::Int) where {T} =
LinearAlgebra.wrapperop(v)(v.parent[i-1+first(axes(v.parent)[1])])
@propagate_inbounds Base.getindex(A::LinearAlgebra.AdjOrTrans{T,<:HSMatrix},
i::Int, j::Int) where {T} = LinearAlgebra.wrapperop(A)(A.parent[j, i])
""" HSA[x1 x2;x3 x4]
A constructor building a static array with heterogeneous values
(stored in a tuple)."""
struct HSA{T} end
Base.getindex(h::Type{HSA}, x...) = similar_type(h, Size(length(x)))(x)
Base.typed_hvcat(h::Type{HSA}, rows::Dims, x...) =
_typed_hvcat_tr(h, rows, x)
Base.typed_hvcat(h::Type{HSA}, rows::Dims, x::Number...) = # disambiguation
_typed_hvcat_tr(h, rows, x)
function _typed_hvcat_tr(h, rows, x)
s = StaticArrays._SA_hvcat_transposed_size(rows)
isnothing(s) && throw(ArgumentError(string(
"HSA[...] matrix with inconsistent row lengths: ", rows)))
similar_type(h, s)(StaticArrays.reorder(x, Val(s[1]), Val(s[2])))
end
(H::Type{HeterogeneousStaticArray{S}})(x::Tuple) where{S} =
StaticArrays.construct_type(H, x)(x)
StaticArrays.similar_type(::Type{HSA}, ::Size{S}) where{S} =
HeterogeneousStaticArray{Tuple{S...}}
StaticArrays.construct_type(::Type{H}, x) where{H<:HeterogeneousStaticArray} =
StaticArrays.adapt_eltype(StaticArrays.adapt_size(H, x), x)
function StaticArrays.adapt_eltype(::Type{HeterogeneousStaticArray{S,D}},
x) where{S,D}
T = StaticArrays.promote_tuple_eltype(x)
return HeterogeneousStaticArray{S,D,T,typeof(x)}
end