# Type instability if function inside a struct is run inside another function

**URL:** <https://discourse.julialang.org/t/type-instability-if-function-inside-a-struct-is-run-inside-another-function/12789>\
**Category:** General Usage\
**Tags:** inference, type\
**Created:** [August 1, 2018, 1:59pm UTC](https://discourse.julialang.org/t/type-instability-if-function-inside-a-struct-is-run-inside-another-function/12789 "2018-08-01T13:59:03Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![zsoerenm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zsoerenm/32/664_2.png) [@zsoerenm](https://discourse.julialang.org/u/zsoerenm)\
**Post date:** [August 1, 2018, 1:59pm UTC](https://discourse.julialang.org/t/type-instability-if-function-inside-a-struct-is-run-inside-another-function/12789/1 "2018-08-01T13:59:03Z")

</div>

Hello,

If I have a function inside a struct and run that function inside another function, the output will be type-unstable.

Example:

```julia
struct StructWithFunction
    func::Function
end
struct_with_function = StructWithFunction(+)
@inferred struct_with_function.func(2,3)
function test(struct_with_function)
    struct_with_function.func(2,3)
end
@code_warntype test(struct_with_function)

```

Output is

```julia
5

Variables:
  #self# <optimized out>
  struct_with_function::StructWithFunction

Body:
  begin 
      return ((Core.getfield)(struct_with_function::StructWithFunction, :func)::F)(2, 3)::Any
  end::Any

```

This is tested with Julia v0.6.4

Anything I can do about it?

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [August 1, 2018, 2:08pm UTC](https://discourse.julialang.org/t/type-instability-if-function-inside-a-struct-is-run-inside-another-function/12789/2 "2018-08-01T14:08:46Z")

</div>

```julia
struct StructWithFunction{F<:Function}
    func::F
end

```

---

<div class="post-metadata">

**Author:** ![tomaklutfu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomaklutfu/32/2411_2.png) [@tomaklutfu](https://discourse.julialang.org/u/tomaklutfu)\
**Post date:** [August 1, 2018, 6:24pm UTC](https://discourse.julialang.org/t/type-instability-if-function-inside-a-struct-is-run-inside-another-function/12789/3 "2018-08-01T18:24:01Z")

</div>

As a side note, `Function` is an abstract type and as other abstract types they are usually slow as direct field types.

---

<div class="post-metadata">

**Author:** ![ExpandingMan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/expandingman/32/866_2.png) [@ExpandingMan](https://discourse.julialang.org/u/ExpandingMan)\
**Post date:** [August 1, 2018, 6:27pm UTC](https://discourse.julialang.org/t/type-instability-if-function-inside-a-struct-is-run-inside-another-function/12789/4 "2018-08-01T18:27:28Z")

</div>

As an interesting side note we’ve recently lost the ability to efficiently unbox functions from containers as currently [FunctionWrappers.jl](https://github.com/yuyichao/FunctionWrappers.jl) is required for that and it only works on 0.6. I’d be interested to know if functors of some kind would be efficient as a temporary work around.

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [August 1, 2018, 7:19pm UTC](https://discourse.julialang.org/t/type-instability-if-function-inside-a-struct-is-run-inside-another-function/12789/5 "2018-08-01T19:19:26Z")

</div>

There’s a PR to fix FunctionWrappers.jl pending, but it’s been stalled waiting for code review for about 3 weeks: [Fixes for Julia 0.7. Drop 0.5 and 0.6. by tkoolen · Pull Request #7 · yuyichao/FunctionWrappers.jl · GitHub](https://github.com/yuyichao/FunctionWrappers.jl/pull/7)

---

<div class="post-metadata">

**Author:** ![ExpandingMan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/expandingman/32/866_2.png) [@ExpandingMan](https://discourse.julialang.org/u/ExpandingMan)\
**Post date:** [August 1, 2018, 7:32pm UTC](https://discourse.julialang.org/t/type-instability-if-function-inside-a-struct-is-run-inside-another-function/12789/6 "2018-08-01T19:32:04Z")

</div>

> [@rdeits](#):
>
> There’s a PR to fix FunctionWrappers.jl pending

Yay! 🎉 At this point I’m just glad it’s there because honestly I had absolutely _no clue_ how to do it myself. I checked to see if there were any in the past but I guess not recently. It’s scary not to at least have that functionality in a functional language like Julia. I don’t use it that often but when I do it doesn’t seem like there are many alternatives.

It works on the linux nightlies, that’s all I need 😄

(As a fun example of where there seemed to be no good alternatives see my [MOS 6502 simulator](https://github.com/ExpandingMan/Sim6502.jl).)
