# Modify array of functions in a mutable struct

**URL:** <https://discourse.julialang.org/t/modify-array-of-functions-in-a-mutable-struct/49748>\
**Category:** New to Julia\
**Tags:** question, array, function, struct\
**Created:** [November 7, 2020, 5:09pm UTC](https://discourse.julialang.org/t/modify-array-of-functions-in-a-mutable-struct/49748 "2020-11-07T17:09:30Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Elmo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elmo/32/17979_2.png) [@Elmo](https://discourse.julialang.org/u/Elmo)\
**Post date:** [November 7, 2020, 5:09pm UTC](https://discourse.julialang.org/t/modify-array-of-functions-in-a-mutable-struct/49748/1 "2020-11-07T17:09:30Z")

</div>

I am trying to create a struct that contains functions, which will be used to construct a set of differential equations. The complication is that I want to be able to modify the struct’s functions after evaluating the solution of the ODE of these functions. Below is a minimal working example of what I mean:

```julia
mutable struct Foo
    farr :: Array{Function, 1} # array of functions used to construct a system of ODEs
end

function initfoo() # create "basic" functions
    vs = Array{Function, 1}(undef, 2)
    f(x) = x^2
    g(x) = 1
    vs[1] = f
    vs[2] = g
    Foo(vs) # return new Foo object
end

foo = initfoo() # create Foo object

# Test the original functions
foo.farr[1](2) # == 4
foo.farr[2](2) # == 1 # these work

# Now I want to change the functions
foo.farr[1] = x -> foo.farr[1](x) * (1 + x)
foo.farr[1](1) # this results in a StackOverflowError

```

It seems I am doing something wrong when I modify the functions in the struct. For my application it is important that I “append” to the original function in the struct. Does anybody know how to fix this?

---

<div class="post-metadata">

**Author:** ![Jeff\_Emanuel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff_emanuel/32/15440_2.png) [@Jeff\_Emanuel](https://discourse.julialang.org/u/Jeff_Emanuel)\
**Post date:** [November 7, 2020, 5:21pm UTC](https://discourse.julialang.org/t/modify-array-of-functions-in-a-mutable-struct/49748/2 "2020-11-07T17:21:02Z")

</div>

```julia
old = foo.farr[1]
foo.farr[1] = x -> old(x) * (1+x)

```

---

<div class="post-metadata">

**Author:** ![Elmo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elmo/32/17979_2.png) [@Elmo](https://discourse.julialang.org/u/Elmo)\
**Post date:** [November 7, 2020, 5:31pm UTC](https://discourse.julialang.org/t/modify-array-of-functions-in-a-mutable-struct/49748/3 "2020-11-07T17:31:29Z")

</div>

I just tried this and it still gave the stack overflow error (on evaluation, not on assignment). I also tried copying the function before re-assigning, but alas that did not work either…

---

<div class="post-metadata">

**Author:** ![Jeff\_Emanuel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff_emanuel/32/15440_2.png) [@Jeff\_Emanuel](https://discourse.julialang.org/u/Jeff_Emanuel)\
**Post date:** [November 7, 2020, 5:36pm UTC](https://discourse.julialang.org/t/modify-array-of-functions-in-a-mutable-struct/49748/4 "2020-11-07T17:36:00Z")

</div>

```julia
julia> vs = Array{Function, 1}(undef, 2)
2-element Array{Function,1}:
 #undef
 #undef

julia> vs[1] = x -> x^2
#4465 (generic function with 1 method)

julia> vs[1](2)
4

julia> old = vs[1]
#4465 (generic function with 1 method)

julia> vs[1] = x -> old(x) * (1+x)
#4467 (generic function with 1 method)

julia> vs[1](2)
12

```

---

<div class="post-metadata">

**Author:** ![Elmo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elmo/32/17979_2.png) [@Elmo](https://discourse.julialang.org/u/Elmo)\
**Post date:** [November 7, 2020, 5:40pm UTC](https://discourse.julialang.org/t/modify-array-of-functions-in-a-mutable-struct/49748/5 "2020-11-07T17:40:48Z")

</div>

Scratch that, it works! Thank you very much!!

---

<div class="post-metadata">

**Author:** ![Jeff\_Emanuel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff_emanuel/32/15440_2.png) [@Jeff\_Emanuel](https://discourse.julialang.org/u/Jeff_Emanuel)\
**Post date:** [November 7, 2020, 5:45pm UTC](https://discourse.julialang.org/t/modify-array-of-functions-in-a-mutable-struct/49748/6 "2020-11-07T17:45:43Z")

</div>

This is more generic and captures the old function in a closure:

```julia
julia> function nextfunc(old,new)
         return x -> new(old(x),x)
         end
nextfunc (generic function with 1 method)

julia> vs[1] = x -> x^2
#4471 (generic function with 1 method)

julia> vs[1] = nextfunc(vs[1],(o,x)->o*(1+x))
#4469 (generic function with 1 method)

julia> vs[1](2)
12

```

---

<div class="post-metadata">

**Author:** ![Elmo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elmo/32/17979_2.png) [@Elmo](https://discourse.julialang.org/u/Elmo)\
**Post date:** [November 7, 2020, 5:52pm UTC](https://discourse.julialang.org/t/modify-array-of-functions-in-a-mutable-struct/49748/7 "2020-11-07T17:52:53Z")

</div>

Thanks 🙂 This will make downstream code prettier I think!
