# How to store functions in a structure in a proper way?

**URL:** <https://discourse.julialang.org/t/how-to-store-functions-in-a-structure-in-a-proper-way/77887>\
**Category:** New to Julia\
**Tags:** question, type, function, struct\
**Created:** [March 14, 2022, 7:01pm UTC](https://discourse.julialang.org/t/how-to-store-functions-in-a-structure-in-a-proper-way/77887 "2022-03-14T19:01:53Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![denshd](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/denshd/32/32247_2.png) [@denshd](https://discourse.julialang.org/u/denshd)\
**Post date:** [March 14, 2022, 7:01pm UTC](https://discourse.julialang.org/t/how-to-store-functions-in-a-structure-in-a-proper-way/77887/1 "2022-03-14T19:01:53Z")

</div>

Hello!

I want to create a structure that stores (besides other data) some functions. It is convinient to store them together in a one structure.

But consider the following code (MWE):

```julia
using BenchmarkTools
struct Foo
    f::Function
end

function calculate(sys::Foo, x::Vector{Float64})::Nothing
    for el in x
        sys.f(el)
    end
end

function main()
    f(x::Float64)::Float64 = sin(x)
    sys = Foo(f)

    N = 100_000
    x = zeros(N)

    @time (
        for el in x
            sys.f(el)
        end
    )

    calculate(sys, x) # compilation
    @time calculate(sys, x)
    @code_warntype calculate(sys, x)
end
main()

```

Notice that when I pass `sys` into `calculate` function and then use sys.f, it **allocates too much**.

`@code_warntype` says:

```julia
MethodInstance for calculate(::Foo, ::Vector{Float64})
    from calculate(sys::Foo, x::Vector{Float64}) in Main at C:\Users\densh\Documents\Science\test.jl:6
  Arguments
    #self#::Core.Const(calculate)
    sys::Foo
    x::Vector{Float64}
  Locals
    @_4::Union{Nothing, Tuple{Float64, Int64}}
    el::Float64
  Body::Nothing
  1 ─ %1 = Main.Nothing::Core.Const(Nothing)
  │ %2 = x::Vector{Float64}
  │ (@_4 = Base.iterate(%2))
  │ %4 = (@_4 === nothing)::Bool
  │ %5 = Base.not_int(%4)::Bool
  └── goto #4 if not %5
  2 ┄ %7 = @_4::Tuple{Float64, Int64}
  │ (el = Core.getfield(%7, 1))
  │ %9 = Core.getfield(%7, 2)::Int64
  │ %10 = Base.getproperty(sys, :f)::Function
  │ (%10)(el)
  │ (@_4 = Base.iterate(%2, %9))
  │ %13 = (@_4 === nothing)::Bool
  │ %14 = Base.not_int(%13)::Bool
  └── goto #4 if not %14
  3 ─ goto #2
  4 ┄ %17 = Base.convert(%1, nothing)::Core.Const(nothing)
  │ %18 = Core.typeassert(%17, %1)::Core.Const(nothing)
  └── return %18

```

where `::Function` is highlighted with red color, which means that `Function` isn’t a non-concrete type (but how is it connected with allocatoins I still don’t understand).

So, my question is what am I do wrong and what is the proper way of “storing functions in a structures” in a sense of performance.

Thanks!

---

<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:** [March 14, 2022, 7:03pm UTC](https://discourse.julialang.org/t/how-to-store-functions-in-a-structure-in-a-proper-way/77887/2 "2022-03-14T19:03:59Z")

</div>

Hello and welcome to the community!

> [@denshd](#):
>
> ```julia
> struct Foo
> f::Function
> end
> 
> ```

Try

```julia
struct Foo{F<:Function} 
    f::F
end

```

To make sure the field is concretely typed.

When the compiler cannot figure out the correct type at compile time you’ll typically see many additional allocations.

---

<div class="post-metadata">

**Author:** ![denshd](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/denshd/32/32247_2.png) [@denshd](https://discourse.julialang.org/u/denshd)\
**Post date:** [March 14, 2022, 7:12pm UTC](https://discourse.julialang.org/t/how-to-store-functions-in-a-structure-in-a-proper-way/77887/3 "2022-03-14T19:12:11Z")

</div>

Thank you a lot! Responded very quickly:)

---

<div class="post-metadata">

**Author:** ![markmbaum](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/markmbaum/32/32745_2.png) [@markmbaum](https://discourse.julialang.org/u/markmbaum)\
**Post date:** [March 14, 2022, 7:32pm UTC](https://discourse.julialang.org/t/how-to-store-functions-in-a-structure-in-a-proper-way/77887/4 "2022-03-14T19:32:51Z")

</div>

There are also a few options if you have multiple functions. Easiest would be

```julia
struct Foo{F<:Function,G<:Function}
    f::F
    g::G
end

```

just in case the multiple parameter syntax isn’t familiar yet 🙂
