# Solution for redefinition of functions being used in other functions?

**URL:** <https://discourse.julialang.org/t/solution-for-redefinition-of-functions-being-used-in-other-functions/469>\
**Category:** General Usage\
**Tags:** question\
**Created:** [November 21, 2016, 1:42pm UTC](https://discourse.julialang.org/t/solution-for-redefinition-of-functions-being-used-in-other-functions/469 "2016-11-21T13:42:10Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![angelv](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/angelv/32/133_2.png) [@angelv](https://discourse.julialang.org/u/angelv)\
**Post date:** [November 21, 2016, 1:42pm UTC](https://discourse.julialang.org/t/solution-for-redefinition-of-functions-being-used-in-other-functions/469/1 "2016-11-21T13:42:10Z")

</div>

Hi,  
I’ve searched around the old mailing lists, and I understand that this is a well-known issue, and that I think it is solved? for v 0.6, but for those still at 0.5, is there any solution/hack/advice?

For example, working with IJulia, I just want to plot a function

```julia
f(x) = tan(x) 
plot(f,-π,π)

```

then change the definition of f(x) and plot again. But plot will keep the old definition of f, despite f itself having changed. At present what I do is restart the IJulia kernel, but obviously this is very annoying if the notebook is anything longer than a toy example, because it takes a long time to restart it again.

Is there anything I can do to avoid restarting the kernel?

Thanks,  
Ángel

---

<div class="post-metadata">

**Author:** ![akis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/akis/32/156_2.png) [@akis](https://discourse.julialang.org/u/akis)\
**Post date:** [November 21, 2016, 1:46pm UTC](https://discourse.julialang.org/t/solution-for-redefinition-of-functions-being-used-in-other-functions/469/2 "2016-11-21T13:46:31Z")

</div>

Check [this](https://discourse.julialang.org/t/overwriting-functions/404) FAQ entry and overall thread.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [November 21, 2016, 2:20pm UTC](https://discourse.julialang.org/t/solution-for-redefinition-of-functions-being-used-in-other-functions/469/3 "2016-11-21T14:20:19Z")

</div>

It appears the change is too big to be backported to 0.5.1, so you have basically two options:

1. use latest master,
2. as a workaround for 0.5, organize your workflow into modules so that related functions recompile together.  
[https://github.com/JuliaLang/julia/issues/265](https://github.com/JuliaLang/julia/issues/265)

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [November 21, 2016, 3:05pm UTC](https://discourse.julialang.org/t/solution-for-redefinition-of-functions-being-used-in-other-functions/469/4 "2016-11-21T15:05:46Z")

</div>

There is a simple solution: use anonymous functions:

```julia
f(x) = tan(x) 
plot(x -> f(x), -π, π)

f(x) = sin(x)
plot(x -> f(x), -π, π)

```

This works because each time you use an anonymous function, it creates a _new_ function:

```julia
julia> f(x) = 3x
f (generic function with 1 method)

julia> x -> f(x)
(::#15) (generic function with 1 method)

julia> x -> f(x)
(::#17) (generic function with 1 method)

```

---

<div class="post-metadata">

**Author:** ![angelv](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/angelv/32/133_2.png) [@angelv](https://discourse.julialang.org/u/angelv)\
**Post date:** [November 21, 2016, 6:15pm UTC](https://discourse.julialang.org/t/solution-for-redefinition-of-functions-being-used-in-other-functions/469/5 "2016-11-21T18:15:26Z")

</div>

Hi,

> There is a simple solution: use anonymous functions:
> 
> f(x) = tan(x)  
> plot(x → f(x), -π, π)
> 
> f(x) = sin(x)  
> plot(x → f(x), -π, π)

this is a great workaround, thanks a lot!

> This works because each time you use an anonymous function, it  
> creates a new function:
> 
> julia\> f(x) = 3x  
> f (generic function with 1 method)
> 
> julia\> x → f(x)  
> (::#15) (generic function with 1 method)
> 
> julia\> x → f(x)  
> (::#17) (generic function with 1 method)

I assume unused anonymous functions will be cleaned at some point by the  
GC?

Thanks a lot,  
Ángel de Vicente

---

<div class="post-metadata">

**Author:** ![akis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/akis/32/156_2.png) [@akis](https://discourse.julialang.org/u/akis)\
**Post date:** [November 21, 2016, 7:26pm UTC](https://discourse.julialang.org/t/solution-for-redefinition-of-functions-being-used-in-other-functions/469/6 "2016-11-21T19:26:02Z")

</div>

> [@angelv](#):
>
> I assume unused anonymous functions will be cleaned at some point by the  
> GC?

Anything unreferenced becomes “garbage” and gets “recycled” at some point by the Garbage Collector. Creating anonymous functions all the time isn’t ideal (even though Julia has drastically decreased their cost). But it’s faster beyond any measure than restarting the kernel, and it’s fine to happen at the pace of a human hitting a button. After all, human time is more valuable than computer time (as long as the computer usage doesn’t come with a high cost itself).

---

<div class="post-metadata">

**Author:** ![johnmyleswhite](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johnmyleswhite/32/31_2.png) [@johnmyleswhite](https://discourse.julialang.org/u/johnmyleswhite)\
**Post date:** [November 21, 2016, 7:52pm UTC](https://discourse.julialang.org/t/solution-for-redefinition-of-functions-being-used-in-other-functions/469/7 "2016-11-21T19:52:45Z")

</div>

> [@akis](#):
>
> Anything unreferenced becomes “garbage” and gets “recycled” at some point by the Garbage Collector.

Can you provide a link to the part of the codebase where Julia calculates when an anonymous function has become unreferenced?

---

<div class="post-metadata">

**Author:** ![akis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/akis/32/156_2.png) [@akis](https://discourse.julialang.org/u/akis)\
**Post date:** [November 21, 2016, 9:08pm UTC](https://discourse.julialang.org/t/solution-for-redefinition-of-functions-being-used-in-other-functions/469/8 "2016-11-21T21:08:21Z")

</div>

> [@johnmyleswhite](#):
>
> Can you provide a link to the part of the codebase where Julia calculates when an anonymous function has become unreferenced?

I rarely follow the development of Julia at code-level. My closest memory on the matter is [this conversation](https://github.com/JuliaLang/julia/pull/13412) and things linked from there, which are a long reading, as anonymous functions have been an important and frequent topic, and their implementation has been a field for competitive approaches. I would be very interested in a more informed answer, as an unrecycled closure could be referencing other objects and affect performance.

---

<div class="post-metadata">

**Author:** ![ScottPJones](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/scottpjones/32/146_2.png) [@ScottPJones](https://discourse.julialang.org/u/ScottPJones)\
**Post date:** [November 23, 2016, 1:49am UTC](https://discourse.julialang.org/t/solution-for-redefinition-of-functions-being-used-in-other-functions/469/9 "2016-11-23T01:49:02Z")

</div>

I had heard once, I believe it was from Jeff, that JITed code was not GCed (at least currently) in Julia.  
Maybe after @vtjnash fixes #265 in v0.6, it will be possible to reclaim some of that memory.

---

<div class="post-metadata">

**Author:** ![akis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/akis/32/156_2.png) [@akis](https://discourse.julialang.org/u/akis)\
**Post date:** [November 23, 2016, 4:30am UTC](https://discourse.julialang.org/t/solution-for-redefinition-of-functions-being-used-in-other-functions/469/10 "2016-11-23T04:30:41Z")

</div>

> [@ScottPJones](#):
>
> JITed code was not GCed (at least currently) in Julia

Thanks for the input. You are most probably right about both the “code” part and the “not yet” part. Similar situation with the unused Symbols. Of course those aren’t the kind of things people lose their sleep over (some other languages don’t even bother to get them right). And they are not design neglects postponed for the distant future, they are conscious intermediate choices planned to have their implementation fixed on later development stages, after more fundamental parts of the language get crystallized. It is the “data” part which causes the headaches and I haven’t yet run in a case where GC let me down on Julia 5.0. The developers have done a great job.

Still it’s sad that some of the involved people opt to treat such information like a privilege for the ones who have the time to read through tones of mixed low-level discussion (not to mention information hidden in unsearchable videos and papers). It can make the experience of people new to the language quite a challenge. Hopefully, the new discussion platform will help the rest of the community to gradually properly document things of any level in an organized way. Thanks @angelv for bringing that one in attention. By the way, I’ve included @dpsanders’ suggestion in the respective FAQ.
