# Type Instability using StaticArrays that I dont get

**URL:** <https://discourse.julialang.org/t/type-instability-using-staticarrays-that-i-dont-get/109499>\
**Category:** Performance\
**Created:** [January 31, 2024, 7:48am UTC](https://discourse.julialang.org/t/type-instability-using-staticarrays-that-i-dont-get/109499 "2024-01-31T07:48:50Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Christian\_Dengler](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christian_dengler/32/20969_2.png) [@Christian\_Dengler](https://discourse.julialang.org/u/Christian_Dengler)\
**Post date:** [January 31, 2024, 7:48am UTC](https://discourse.julialang.org/t/type-instability-using-staticarrays-that-i-dont-get/109499/1 "2024-01-31T07:48:50Z")

</div>

Hello,  
I have a type instability according to `@code_warntype` in the following code. To me it seems all types should be inferable though. What would I need to change to make it type stable?

```julia
using StaticArrays
import LinearAlgebra: norm

struct Line{T}
    p1::StaticVector{2, T}
    p2::StaticVector{2, T}
end

function closest_point_on_line(p::SVector{2, T}, line::Line{T}) where {T}
    length_squared = (line.p1[1]-line.p2[1])^2 + (line.p1[2]-line.p2[2])^2
    t = clamp(dot( p - line.p1, line.p2 - line.p1 )/length_squared, zero(T), one(T))
    return line.p1 + t*(line.p2 - line.p1)
end

@code_warntype closest_point_on_line(SVector(0.0, 1.0), Line(SVector(1.0, 0.0), SVector(1.0, 1.0)))

```

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [January 31, 2024, 7:53am UTC](https://discourse.julialang.org/t/type-instability-using-staticarrays-that-i-dont-get/109499/2 "2024-01-31T07:53:57Z")

</div>

```julia-repl
julia> isconcretetype(StaticVector{2, Float64})
false

julia> isconcretetype(SVector{2, Float64})
true

```

---

<div class="post-metadata">

**Author:** ![Christian\_Dengler](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christian_dengler/32/20969_2.png) [@Christian\_Dengler](https://discourse.julialang.org/u/Christian_Dengler)\
**Post date:** [January 31, 2024, 7:57am UTC](https://discourse.julialang.org/t/type-instability-using-staticarrays-that-i-dont-get/109499/3 "2024-01-31T07:57:27Z")

</div>

Oh ok, I somehow assumed `StaticVector` would be an alias of `SVector`. Thanks for that tip. One instability remains though. Apparently a dot product of 2 `SVector` s gives an `Any`. I guess there is not much i can do about that, except assert the type of the result?

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [January 31, 2024, 8:02am UTC](https://discourse.julialang.org/t/type-instability-using-staticarrays-that-i-dont-get/109499/4 "2024-01-31T08:02:58Z")

</div>

> [@Christian\_Dengler](#):
>
> Apparently a dot product of 2 `SVector` s gives an `Any`

It’s type stable for me

```julia
julia> @code_warntype dot(a, b)
MethodInstance for LinearAlgebra.dot(::SVector{2, Float64}, ::SVector{2, Float64})
  from dot(a::StaticArray, b::StaticArray) @ StaticArrays ~/.julia/packages/StaticArrays/oOCPP/src/linalg.jl:208
Arguments
  #self#::Core.Const(LinearAlgebra.dot)
  a::SVector{2, Float64}
  b::SVector{2, Float64}
Body::Float64
1 ─ nothing
│ %2 = StaticArrays.same_size(a, b)::Core.Const(Size(2,))
│ %3 = StaticArrays._vecdot(%2, a, b, StaticArrays.dot)::Float64
└── return %3

```

Even if it would be type unstable, you could also implement is as `a'b` or using a loop etc., there would be plenty of options 😉

---

<div class="post-metadata">

**Author:** ![barucden](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/barucden/32/26154_2.png) [@barucden](https://discourse.julialang.org/u/barucden)\
**Post date:** [January 31, 2024, 8:03am UTC](https://discourse.julialang.org/t/type-instability-using-staticarrays-that-i-dont-get/109499/5 "2024-01-31T08:03:30Z")

</div>

You only imported `norm` from `LinearAlgebra` and forgot about `dot`.
