# Why can I not add methods to a function with a value?

**URL:** <https://discourse.julialang.org/t/why-can-i-not-add-methods-to-a-function-with-a-value/40819>\
**Category:** General Usage\
**Tags:** question\
**Created:** [June 5, 2020, 4:00pm UTC](https://discourse.julialang.org/t/why-can-i-not-add-methods-to-a-function-with-a-value/40819 "2020-06-05T16:00:50Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![derikk](https://avatars.discourse-cdn.com/v4/letter/d/76d3ee/32.png) [@derikk](https://discourse.julialang.org/u/derikk)\
**Post date:** [June 5, 2020, 4:00pm UTC](https://discourse.julialang.org/t/why-can-i-not-add-methods-to-a-function-with-a-value/40819/1 "2020-06-05T16:00:50Z")

</div>

I was trying to add a method to a function that accepts strings but not integers to convert integers to strings if necessary.  
For instance, given `f = reverse`, I get 16 methods, none of which apply to numerical types.

Now, I want to add `f(x::Integer) = reverse(string(x))`. I would expect that this would add a new method for `Integer`s that would for example map `123` to `"321"`.

However, I get an error:  
`ERROR: cannot define function f; it already has a value`

Even though there would be no conflict in the methods assigned to `f`, Julia doesn’t let me add this additional method to it because I originally defined it as equal to `reverse`.

I thought this might be a result of shallow copying; however, I tried `g = deepcopy(reverse)` and got the same results.

Is there any way to clone Julia functions and add methods to them (not extending existing functions)? Is the current behavior a bug, or intended?

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [June 5, 2020, 4:02pm UTC](https://discourse.julialang.org/t/why-can-i-not-add-methods-to-a-function-with-a-value/40819/2 "2020-06-05T16:02:10Z")

</div>

You probably have assigned something to `f` already (maybe `reverse`?).

---

<div class="post-metadata">

**Author:** ![derikk](https://avatars.discourse-cdn.com/v4/letter/d/76d3ee/32.png) [@derikk](https://discourse.julialang.org/u/derikk)\
**Post date:** [June 5, 2020, 4:03pm UTC](https://discourse.julialang.org/t/why-can-i-not-add-methods-to-a-function-with-a-value/40819/3 "2020-06-05T16:03:49Z")

</div>

Yes, I have assigned the 16 methods of `reverse` to the function `f` already, but I am confused why I cannot add more methods to it that do not conflict with its existing definition.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [June 5, 2020, 4:07pm UTC](https://discourse.julialang.org/t/why-can-i-not-add-methods-to-a-function-with-a-value/40819/4 "2020-06-05T16:07:46Z")

</div>

> [@derikk](#):
>
> Yes, I have assigned the 16 methods of `reverse` to the function `f` already

You need to do `f(x) = reverse(x)`, not `f = reverse`, if you want to add additional methods to `f` (without affecting `Base.reverse`). That is, `f` needs to be a _new_ generic function, not an alias for `Base.reverse`.

If you want to add new methods to `Base.reverse` itself, you need to do `Base.reverse(...new args...) = ...new implementation...`, since you’re not allowed to extend methods in other modules without explicitly qualifying them with the module name (or explicitly importing them).

---

<div class="post-metadata">

**Author:** ![derikk](https://avatars.discourse-cdn.com/v4/letter/d/76d3ee/32.png) [@derikk](https://discourse.julialang.org/u/derikk)\
**Post date:** [June 5, 2020, 4:14pm UTC](https://discourse.julialang.org/t/why-can-i-not-add-methods-to-a-function-with-a-value/40819/5 "2020-06-05T16:14:18Z")

</div>

Got it, thanks!
