# Pass functions to objects

**URL:** <https://discourse.julialang.org/t/pass-functions-to-objects/71505>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [November 15, 2021, 4:01pm UTC](https://discourse.julialang.org/t/pass-functions-to-objects/71505 "2021-11-15T16:01:37Z")\
**Posts on this page:** 3\
**Page:** 2

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [November 15, 2021, 5:16pm UTC](https://discourse.julialang.org/t/pass-functions-to-objects/71505/21 "2021-11-15T17:16:23Z")

</div>

Maybe the functions can dispatch on both the particle type and the trap type? Is that expressive enough?

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [November 15, 2021, 6:57pm UTC](https://discourse.julialang.org/t/pass-functions-to-objects/71505/22 "2021-11-15T18:57:05Z")

</div>

If you use something like:

```julia
mutable struct System{P, Tup}
    ps::Vector{P}
    forces::Tup
end

```

You can allow the user to provide a tuple with many forces, which will allow specialized methods, because the tuple is annotated in every element.

---

<div class="post-metadata">

**Author:** ![ncxst](https://avatars.discourse-cdn.com/v4/letter/n/e47774/32.png) [@ncxst](https://discourse.julialang.org/u/ncxst)\
**Post date:** [November 15, 2021, 8:36pm UTC](https://discourse.julialang.org/t/pass-functions-to-objects/71505/23 "2021-11-15T20:36:42Z")

</div>

Thanks, this does everything I need and reduces the performance difference down to ~20ns.

[Previous page](https://discourse.julialang.org/t/pass-functions-to-objects/71505.md?page=1)
