# Have a function field in a structure, using another field in the structure

**URL:** <https://discourse.julialang.org/t/have-a-function-field-in-a-structure-using-another-field-in-the-structure/74329>\
**Category:** New to Julia\
**Tags:** function, struct\
**Created:** [January 10, 2022, 11:53am UTC](https://discourse.julialang.org/t/have-a-function-field-in-a-structure-using-another-field-in-the-structure/74329 "2022-01-10T11:53:33Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Torkel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/torkel/32/5030_2.png) [@Torkel](https://discourse.julialang.org/u/Torkel)\
**Post date:** [January 10, 2022, 11:53am UTC](https://discourse.julialang.org/t/have-a-function-field-in-a-structure-using-another-field-in-the-structure/74329/1 "2022-01-10T11:53:33Z")

</div>

I remember I could do this in Java, and wondered if there was a way to do it in Julia.

E.g. I have a structure

```julia
mutable struct my_struct
    val::Float64
    func::Function
end

```

where the function will do something with the value. I can do this:

```julia
ms1 = my_struct(1.5,v->v+1)
ms2 = my_struct(2.5,v->v+2)

println("Result 1: $(ms1.func(ms1.val))")
println("Result 2: $(ms2.func(ms2.val))")

```

but is there some clever way of making the function referencing the value, without having to pass it into the argument?

---

<div class="post-metadata">

**Author:** ![cstjean](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cstjean/32/1444_2.png) [@cstjean](https://discourse.julialang.org/u/cstjean)\
**Post date:** [January 10, 2022, 2:18pm UTC](https://discourse.julialang.org/t/have-a-function-field-in-a-structure-using-another-field-in-the-structure/74329/2 "2022-01-10T14:18:10Z")

</div>

You could pass a closure, i.e. `make_my_struct(val, func) = my_struct(val, ()->func(val))`, but the more Julian approach would be to have a function like `apply(ms::MyStruct) = ms.func(ms.val)`, then `println("Result 1: $(apply(ms1)))`.

Does that answer your question? FWIW, storing functions in structs is not exactly uncommon, but there’s often better ways of doing things.

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [January 10, 2022, 4:08pm UTC](https://discourse.julialang.org/t/have-a-function-field-in-a-structure-using-another-field-in-the-structure/74329/3 "2022-01-10T16:08:05Z")

</div>

You definitely don’t want to store a value in the function itself, the function would be too specific to adapt to different values at once. It’s better to store the function and value independently. You do so with a struct, and you could’ve also done so with other containers like Tuples or Arrays.

If you want to save some typing, @cstjean has the right ideas. A functor could reduce typing a bit further. Closure methods like `()->func(val)` are actually currently implemented as a hidden functor struct and method, and in this case we just turn your struct itself into a functor:

```julia
mutable struct OneArg
  val
  func::Function
end

(m::OneArg)() = m.func(m.val)
# this is a method, but m is not a method name
# m references the functor instance

ms = OneArg(1.5,v->v+1)
println(ms()) # 2.5

ms.val = 2
ms.func = v->v+2
println(ms()) # 4

```

It’s best to keep the mutable struct with broad abstract field types (in this case, `::Any` and `::Function`) so you can mutate an instance without running into type conversion errors.

If you end up using an immutable struct and you’re only using a few types of functions and arguments consistently, it might be worth making sure your fields have concrete types

```julia
struct ImmutableOneArg{ValType, FuncType<:Function}
  val::ValType
  func::FuncType
end

```

---

<div class="post-metadata">

**Author:** ![Torkel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/torkel/32/5030_2.png) [@Torkel](https://discourse.julialang.org/u/Torkel)\
**Post date:** [January 10, 2022, 4:25pm UTC](https://discourse.julialang.org/t/have-a-function-field-in-a-structure-using-another-field-in-the-structure/74329/4 "2022-01-10T16:25:04Z")

</div>

Thanks, both!

I think I have to digest this a bit though, looks non-trivial.

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [January 10, 2022, 4:30pm UTC](https://discourse.julialang.org/t/have-a-function-field-in-a-structure-using-another-field-in-the-structure/74329/5 "2022-01-10T16:30:28Z")

</div>

Here’s the (very short) parts of the docs about functors and closures, studying these may help the digestion more than the examples here:

- [Manual \> Methods \> Function-like objects](https://docs.julialang.org/en/v1/manual/methods/#Function-like-objects)
- [Developer Documentation \> Documentation of Julia’s Internals \> Julia Functions \> Closures](https://docs.julialang.org/en/v1/devdocs/functions/#Closures)
