# Overriding methods with closures?

**URL:** <https://discourse.julialang.org/t/overriding-methods-with-closures/36311>\
**Category:** New to Julia\
**Created:** [March 21, 2020, 7:01pm UTC](https://discourse.julialang.org/t/overriding-methods-with-closures/36311 "2020-03-21T19:01:08Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![gleyland](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gleyland/32/15339_2.png) [@gleyland](https://discourse.julialang.org/u/gleyland)\
**Post date:** [March 21, 2020, 7:01pm UTC](https://discourse.julialang.org/t/overriding-methods-with-closures/36311/1 "2020-03-21T19:01:09Z")

</div>

Hi,

Is it possible to override a method with a closure?

```julia
function add(x, y) x + y end
function test()
    add(y) = add(1, y)
    add(2)
end
test()

```

`ERROR: MethodError: no method matching (::var"#add#4")(::Int64, ::Int64)`

The same thing happens if I define add as `function add(y) add(1, y) end`, and it doesn’t seem to help if I declare `global add` as the first line of test. Is this possible, or do I just need to use different names?

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![Vasily\_Pisarev](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vasily_pisarev/32/7929_2.png) [@Vasily\_Pisarev](https://discourse.julialang.org/u/Vasily_Pisarev)\
**Post date:** [March 21, 2020, 7:57pm UTC](https://discourse.julialang.org/t/overriding-methods-with-closures/36311/2 "2020-03-21T19:57:01Z")

</div>

Hi and welcome!

The definition of `add` as a local name shadows all outer definitions.  
To access any global identifier within a scope where that name is already taken, you can prepend them with the module name.  
In your case, assuming you work in module Main:

```julia
function add(x, y) x + y end
function test()
    add(y) = Main.add(1, y)
    add(2)
end
test()

```

---

<div class="post-metadata">

**Author:** ![gleyland](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gleyland/32/15339_2.png) [@gleyland](https://discourse.julialang.org/u/gleyland)\
**Post date:** [March 22, 2020, 5:57am UTC](https://discourse.julialang.org/t/overriding-methods-with-closures/36311/3 "2020-03-22T05:57:21Z")

</div>

Thanks!

That makes sense. In this instance it would be nice if local identifiers could overload ones from outer scopes, but I imagine that the trade-offs around default cases for scopes are fairly tricky.

---

<div class="post-metadata">

**Author:** ![gleyland](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gleyland/32/15339_2.png) [@gleyland](https://discourse.julialang.org/u/gleyland)\
**Post date:** [March 22, 2020, 7:56am UTC](https://discourse.julialang.org/t/overriding-methods-with-closures/36311/4 "2020-03-22T07:56:03Z")

</div>

This implies that there’s still no overloading happening?

That is, if I wish to use the outer `add` in `test()` the only options are either `add(x, y) = Main.add(x, y)` or `Main.add(3, 4)`? There’s no automatic way of having the inner `add` include all the overloads of the outer one?

(There’s no problem with this, I can just rename things or use `Main`. Just wanting to make sure I understand)

Thanks!

---

<div class="post-metadata">

**Author:** ![Vasily\_Pisarev](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vasily_pisarev/32/7929_2.png) [@Vasily\_Pisarev](https://discourse.julialang.org/u/Vasily_Pisarev)\
**Post date:** [March 22, 2020, 8:17am UTC](https://discourse.julialang.org/t/overriding-methods-with-closures/36311/5 "2020-03-22T08:17:33Z")

</div>

Right, there’s no overloading.

The problem here is that local `add` and `Main.add` are two different identifiers. So, the local `add` cannot possibly add overloaded behavior to `Main.add` within local context.  
The local `add`, however, can be overloaded:

```julia
function add(x, y) x + y end
function test()
    add(x, y) = Main.add(x, y)
    add(y) = add(1, y)
    add(y::Nothing) = 1
    return add(2), add(nothing)
end
test() # returns (3, 1)

```

---

<div class="post-metadata">

**Author:** ![ffevotte](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ffevotte/32/6587_2.png) [@ffevotte](https://discourse.julialang.org/u/ffevotte)\
**Post date:** [March 23, 2020, 10:58am UTC](https://discourse.julialang.org/t/overriding-methods-with-closures/36311/6 "2020-03-23T10:58:14Z")

</div>

I’m not sure what you want to achieve, but overloading a local function can be a way to build a function with multiple methods closing over some local state. For example:

```julia
function make_inc()
    c = Ref(0)
    inc(n) = c[]+=n

    inc() = inc(1)

    inc
end

```

```julia
julia> const inc = make_inc()
inc (generic function with 2 methods)

julia> inc()
1

julia> inc()
2

julia> inc(3)
5

```

---

<div class="post-metadata">

**Author:** ![gleyland](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gleyland/32/15339_2.png) [@gleyland](https://discourse.julialang.org/u/gleyland)\
**Post date:** [March 23, 2020, 9:08pm UTC](https://discourse.julialang.org/t/overriding-methods-with-closures/36311/7 "2020-03-23T21:08:32Z")

</div>

Thanks, but not in this case. The use case is really just readability:

```julia
function helper(p1, p2, p3, v1, v2) ... end

function doer(p1, p2, p3)
    helper(v1, v2) = Main.helper(p1, p2, p3, v1, v2)

    helper(va, vb)
    helper(vc, vd)
    ...
end

```

(In retrospect, I probably should have put that in the first post)
