# Type instability using interpolation as field of struct

**URL:** <https://discourse.julialang.org/t/type-instability-using-interpolation-as-field-of-struct/27002>\
**Category:** General Usage\
**Created:** [July 30, 2019, 10:37pm UTC](https://discourse.julialang.org/t/type-instability-using-interpolation-as-field-of-struct/27002 "2019-07-30T22:37:40Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jbmuir](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jbmuir/32/4798_2.png) [@jbmuir](https://discourse.julialang.org/u/jbmuir)\
**Post date:** [July 30, 2019, 10:37pm UTC](https://discourse.julialang.org/t/type-instability-using-interpolation-as-field-of-struct/27002/1 "2019-07-30T22:37:40Z")

</div>

My application has a construct equivalent to the following MWE

```julia
using Interpolations

struct MyStruct{T<:Real, IT<:AbstractInterpolation{T,1}}
    x::Array{T,1}
    y::Array{T,1}
    itp::IT
end

MyStruct(x::Array{T,1},y::Array{T,1}) where T = MyStruct{T,AbstractInterpolation{T,1}}(x,y,LinearInterpolation(x,y))

x = collect(1.0:50.0)
y = randn(50)
m = MyStruct(x,y)

f(m::MyStruct{T, AbstractInterpolation{T,1}}, x::T) where T<:Real = m.itp(x)

```

for which calling

```julia
@code_warntype f(m,1.0) 

```

gives `Any` as the return type. Is there some way to rewrite this so that the return type can be correctly inferred as `T` (i.e. `Float64` in this case)?

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [July 30, 2019, 11:32pm UTC](https://discourse.julialang.org/t/type-instability-using-interpolation-as-field-of-struct/27002/2 "2019-07-30T23:32:48Z")

</div>

Your constructor and method definitions are a bit odd (over specified).

Using

```julia
MyStruct(x::Array{T,1}, y::Array{T,1}) where T = MyStruct(x,y,LinearInterpolation(x,y))

f(m::MyStruct{T}, x::T) where T<:Real = m.itp(x)

```

and things are ok.

Why did you shove the `AbstractInterpolation{T,1}` part into the constructor call?

---

<div class="post-metadata">

**Author:** ![jbmuir](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jbmuir/32/4798_2.png) [@jbmuir](https://discourse.julialang.org/u/jbmuir)\
**Post date:** [July 30, 2019, 11:42pm UTC](https://discourse.julialang.org/t/type-instability-using-interpolation-as-field-of-struct/27002/3 "2019-07-30T23:42:17Z")

</div>

Proximally, the original struct has two parametric type parameters and I assumed you needed to specify both of them in the constructor. Ultimately, I still don’t have a good grasp of how to use Julia’s parametric type system (rather embarrassingly given the cake symbol I just noticed next to my name)

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [July 30, 2019, 11:48pm UTC](https://discourse.julialang.org/t/type-instability-using-interpolation-as-field-of-struct/27002/4 "2019-07-30T23:48:59Z")

</div>

If the parameter can be determined from the inputs there is no need to specify it.

```julia
julia> struct S{T}
           x::T
       end

julia> S(2)
S{Int64}(2)

```

---

<div class="post-metadata">

**Author:** ![jbmuir](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jbmuir/32/4798_2.png) [@jbmuir](https://discourse.julialang.org/u/jbmuir)\
**Post date:** [July 30, 2019, 11:54pm UTC](https://discourse.julialang.org/t/type-instability-using-interpolation-as-field-of-struct/27002/5 "2019-07-30T23:54:25Z")

</div>

Ok, that is useful to know. In this case, the constructor is also doing the extra work of doing the interpolation. The thing that confuses me is that if I do `@code_warntype m.itp(x)`, that is fine, but `f(m,x)` is not even when the type information is present in `m`; I think this is quite similar to the question from [Type instability if function inside a struct is run inside another function](https://discourse.julialang.org/t/type-instability-if-function-inside-a-struct-is-run-inside-another-function/12789) , and my original solution was an attempt to emulate the accepted solution from that question

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [July 30, 2019, 11:58pm UTC](https://discourse.julialang.org/t/type-instability-using-interpolation-as-field-of-struct/27002/6 "2019-07-30T23:58:13Z")

</div>

> [@jbmuir](#):
>
> In this case, the constructor is also doing the extra work of doing the interpolation.

Sure but it isn’t really anything different from

```julia
julia> struct S2{T, P}
           x::T
           y::P
       end

julia> S2(x) = S2(x, sin(x))
S2

julia> S2(1)
S2{Int64,Float64}(1, 0.8414709848078965)

```

The problem in your example was that there was “bad” type information in `m`. What was put as `IT` was `AbstractInterpolation` which the compiler cannot reason about. What we wanted in there was the concrete type of the interpolation object that we created.

---

<div class="post-metadata">

**Author:** ![jbmuir](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jbmuir/32/4798_2.png) [@jbmuir](https://discourse.julialang.org/u/jbmuir)\
**Post date:** [July 31, 2019, 12:00am UTC](https://discourse.julialang.org/t/type-instability-using-interpolation-as-field-of-struct/27002/7 "2019-07-31T00:00:20Z")

</div>

Thanks - I really appreciate the example; a good Julia birthday present
