# @code_warntype with StaticArrays

I am initializing a `MVector` and `MMatrix` from `StaticArrays` inside a function as follows

``````function test(v::Vector{T}) where{T<:Real}
n = length(v)
myvec = MVector{n, T}(undef)
mymat = MMatrix{n, n, T}(undef)

...

end
``````

If I do

``````v=[1.0, 2.0]
@code_warntype test(v)
``````

I get output that begins

``````Static Parameters
T = Float64
Arguments
#self#::Core.Const(test)
v::Vector{Float64}
Locals
mymat::Any
myvec::MArray{_A, Float64, 1} where _A<:Tuple
n::Int64
Body::Any
``````

What am I doing wrong when I initialize my vector and matrix? Thanks.

StaticArrays are type-inferred if the size is known at compile time, not runtime. In your case, `n` is only known at run-time, so the type can’t be inferred. Moreover, in the `MMatrix` constructor, you need to specify the fourth parameter, which is the length of the array `n^2`.

1 Like