# Function inside struct allocates when referenced

**URL:** <https://discourse.julialang.org/t/function-inside-struct-allocates-when-referenced/105785>\
**Category:** New to Julia\
**Tags:** function\
**Created:** [November 4, 2023, 4:55am UTC](https://discourse.julialang.org/t/function-inside-struct-allocates-when-referenced/105785 "2023-11-04T04:55:37Z")\
**Posts on this page:** 1\
**Showing post:** 4

<div class="post-metadata">

**Author:** ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/27854_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)\
**Post date:** [November 4, 2023, 11:17am UTC](https://discourse.julialang.org/t/function-inside-struct-allocates-when-referenced/105785/4 "2023-11-04T11:17:36Z")

</div>

So in concrete terms, this is what a more efficient `Box` would look like:

```julia
struct Box{F}
    func::F
end

```

No need to specify `F <: Function`, since there are objects that are not functions but still callable.

However, this trick will not work well for `func_array`. Indeed, each function `f` in Julia has its own type `typeof(f)`, and so you cannot specify a concrete type for the array elements unless all of its elements have the same type, i.e. they are the same function. Which would make said array useless.  
Why do you need an array of functions in your use case?

---

_[View the full topic](https://discourse.julialang.org/t/function-inside-struct-allocates-when-referenced/105785)._
