# Updating a function in a type, where the function is a parameter

**URL:** <https://discourse.julialang.org/t/updating-a-function-in-a-type-where-the-function-is-a-parameter/79933>\
**Category:** Performance\
**Created:** [April 24, 2022, 8:24am UTC](https://discourse.julialang.org/t/updating-a-function-in-a-type-where-the-function-is-a-parameter/79933 "2022-04-24T08:24:52Z")\
**Posts on this page:** 7\
**Page:** 2

<div class="post-metadata">

**Author:** ![FPGro](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fpgro/32/20822_2.png) [@FPGro](https://discourse.julialang.org/u/FPGro)\
**Post date:** [April 24, 2022, 1:09pm UTC](https://discourse.julialang.org/t/updating-a-function-in-a-type-where-the-function-is-a-parameter/79933/21 "2022-04-24T13:09:55Z")

</div>

Did you decide how to continue yet? (stated differently: is your question answered or is there anything else to consider?)

---

<div class="post-metadata">

**Author:** ![kellertuer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kellertuer/32/220707_2.png) [@kellertuer](https://discourse.julialang.org/u/kellertuer)\
**Post date:** [April 24, 2022, 2:10pm UTC](https://discourse.julialang.org/t/updating-a-function-in-a-type-where-the-function-is-a-parameter/79933/22 "2022-04-24T14:10:29Z")

</div>

Eh, the stated differently question has a different answer from the first one 😄

I think I have an answer to the question, that the idea I had is not possible the way I had in mind. So there is nothing else to consider.

In the point of how to continue, I am not yet sure, because I think my `MyStruct` (or `Problem`) has to either be minimised (to just have functions in there) - then I could even to a copy and stay with structs (since I would end up having about 4 different structs for functions, maybe 5 or 6).  
But that would be so much work for the package, since this struct is such a central thing, that for now I might just not do this feature and maybe think about another way to do iterative optimisation where the sub solver changes. I do not know yet.

So no; I do not know hot to continue yet.

And for the other questions whether this is answered: Yes, it is. It‘s not possible.

---

<div class="post-metadata">

**Author:** ![goerch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerch/32/29122_2.png) [@goerch](https://discourse.julialang.org/u/goerch)\
**Post date:** [April 24, 2022, 2:22pm UTC](https://discourse.julialang.org/t/updating-a-function-in-a-type-where-the-function-is-a-parameter/79933/23 "2022-04-24T14:22:52Z")

</div>

I have seen two approaches for typing functions in the past: [MethodWrappers](https://gist.github.com/cscherrer/df2fd33b7dbda3b3aba82a28e3a8a54e) or (with even more black magic) [FunctionWrappers](https://github.com/yuyichao/FunctionWrappers.jl). Could one of these help?

---

<div class="post-metadata">

**Author:** ![kellertuer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kellertuer/32/220707_2.png) [@kellertuer](https://discourse.julialang.org/u/kellertuer)\
**Post date:** [April 24, 2022, 2:46pm UTC](https://discourse.julialang.org/t/updating-a-function-in-a-type-where-the-function-is-a-parameter/79933/24 "2022-04-24T14:46:30Z")

</div>

That looks interesting, Wrapping the function was one Idea I mentioned above but I am not yet sure how to get that to work. But I will take a look how it is done there.

---

<div class="post-metadata">

**Author:** ![kellertuer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kellertuer/32/220707_2.png) [@kellertuer](https://discourse.julialang.org/u/kellertuer)\
**Post date:** [April 25, 2022, 8:13am UTC](https://discourse.julialang.org/t/updating-a-function-in-a-type-where-the-function-is-a-parameter/79933/25 "2022-04-25T08:13:27Z")

</div>

My first approach I might try is to model the function itself as a functor

```julia
mutable struct MyFun{F}
   f::F
   lambda:Float664
end

(f::MyFun)(x) = lambda * x

```

well something along those lines since the things I want to change are not arbitrary but structured with parameters. That way I do not have to replace the function but can modify it (or its parameters to be precise) still.

---

<div class="post-metadata">

**Author:** ![FPGro](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fpgro/32/20822_2.png) [@FPGro](https://discourse.julialang.org/u/FPGro)\
**Post date:** [April 25, 2022, 8:22am UTC](https://discourse.julialang.org/t/updating-a-function-in-a-type-where-the-function-is-a-parameter/79933/26 "2022-04-25T08:22:15Z")

</div>

> [@kellertuer](#):
>
> since the things I want to change are not arbitrary but structured with parameters. That way I do not have to replace the function but can modify it

Sounds good, that’s kind of what my first suggestion was aiming at IIUC.  
Small thing, in:

> [@kellertuer](#):
>
> ```julia-auto
> mutable struct MyFun{F}
> f::F
> lambda:Float664
> end
> 
> (f::MyFun)(x) = lambda * x
> 
> ```

The stored function `f` is not actually used. The `(f::MyFun)(x) = ...` makes instances of the MyFun type callable, to use the stored `f` you still have to `f.f(...)` in this case.

---

<div class="post-metadata">

**Author:** ![kellertuer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kellertuer/32/220707_2.png) [@kellertuer](https://discourse.julialang.org/u/kellertuer)\
**Post date:** [April 25, 2022, 8:30am UTC](https://discourse.julialang.org/t/updating-a-function-in-a-type-where-the-function-is-a-parameter/79933/27 "2022-04-25T08:30:03Z")

</div>

> [@FPGro](#):
>
> The stored function `f` is not actually used. The `(f::MyFun)(x) = ...` makes instances of the MyFun type callable, to use the stored `f` you still have to `f.f(...)` in this case.

…the main reason that it is not used is; Me being too fast in typing the example 😉

Sure what I meant was more like

```julia
(F::MyFun)(x) = (1-lambda) * F.f(x) - lambda

```

[Previous page](https://discourse.julialang.org/t/updating-a-function-in-a-type-where-the-function-is-a-parameter/79933.md?page=1)
