How do I extract number of dimensions and type from a StaticArray?

Hello!

Suppose I have:

    n, d = 100, 2
    p = rand(SVector{d,Float64},n)

 p = rand(SVector{d,Float64},n)
100-element Vector{SVector{2, Float64}}:
 [0.45095181425482966, 0.07762277325569156]
 [0.08059534744395058, 0.6894885967075672]
 [0.17420764849307047, 0.5389305800109263]
 [0.49059524232908114, 0.8173513178012474]
 [0.10571769568776879, 0.10357908367697921]
 [0.9792467310066209, 0.23057688418253763]
 [0.9847249018638312, 0.217772010510753]
 [0.0840194679091536, 0.504223015371956]
 [0.01633520205319361, 0.9021979319615324]
 ⋮
 [0.06984810450766477, 0.9596269881836494]
 [0.31785593598143047, 0.9321562497383631]
 [0.6830008727269754, 0.2723801423965435]
 [0.10025068872213305, 0.49455865902837015]
 [0.5442057537990393, 0.7884480165567407]
 [0.4287597478514289, 0.5612363821439627]
 [0.46923631245452835, 0.4326449125447476]
 [0.05712658935227466, 0.8437525317817943]

How would I go about pulling out from the type definition, SVector{2, Float64}, that dimensions = 2 and type = Float64?

Thank you

julia> getspecs1(::SVector{d,T}) where {d,T} = (dimensions=d, type=T)
getspecs1 (generic function with 1 method)

julia> getspecs1(p[1])
(dimensions = 2, type = Float64)

julia> getspecs2(::Type{SVector{d,T}}) where {d,T} = (dimensions=d, type=T)
getspecs2 (generic function with 1 method)

julia> getspecs2(SVector{2,Float64})
(dimensions = 2, type = Float64)

Perfect, I was not aware one could infer like that :slight_smile: Cool!

And here is how to call it for my case;

getspecs2(eltype(p))
(dimensions = 2, type = Float64)
1 Like

You can also do this (and variants thereof):

julia> getspecs3(::Vector{SVector{d,T}}) where {d,T} = (dimensions=d, type=T)
getspecs3 (generic function with 1 method)

julia> getspecs3(p)
(dimensions = 2, type = Float64)
1 Like