# Invoke a method

**URL:** <https://discourse.julialang.org/t/invoke-a-method/1275>\
**Category:** General Usage\
**Tags:** question\
**Created:** [January 4, 2017, 11:44am UTC](https://discourse.julialang.org/t/invoke-a-method/1275 "2017-01-04T11:44:10Z")\
**Posts on this page:** 12\
**Page:** 1

<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:** [January 4, 2017, 11:44am UTC](https://discourse.julialang.org/t/invoke-a-method/1275/1 "2017-01-04T11:44:10Z")

</div>

Is it possible to invoke a `Method`? Ie suppose I picked one from the `MethodList` returned by `methods`, and I want to invoke that particular method with given arguments. Eg

```julia
m=which((+),(Int,Int))
# now how to call m with (1,2)?

```

I tried simply calling it as `m(1,2)`, but apparently `Method` is not callable per se.

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [January 4, 2017, 12:20pm UTC](https://discourse.julialang.org/t/invoke-a-method/1275/2 "2017-01-04T12:20:28Z")

</div>

`invoke`? [21 chars…]

---

<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:** [January 4, 2017, 12:36pm UTC](https://discourse.julialang.org/t/invoke-a-method/1275/3 "2017-01-04T12:36:19Z")

</div>

how do I use that?

```julia
julia> invoke(which((+),Tuple{Int,Int}),1,2)
ERROR: TypeError: Method: in invoke, expected Type{T}, got Int64

```

the help for `invoke` suggests that I could omit the type sig, but I could not make it work even with that:

```julia
julia> invoke(which((+),Tuple{Int,Int}),(Any,Any),1,2)
ERROR: MethodError: objects of type Method are not callable

```

Using `v"0.5.0"`.

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [January 4, 2017, 12:52pm UTC](https://discourse.julialang.org/t/invoke-a-method/1275/4 "2017-01-04T12:52:08Z")

</div>

Call invoke on the function, not the method:

```julia
invoke(+, Tuple{Int,Int}, 1,2)

```

---

<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:** [January 4, 2017, 12:58pm UTC](https://discourse.julialang.org/t/invoke-a-method/1275/5 "2017-01-04T12:58:01Z")

</div>

@mauro3: I am aware of this, I was just asking how @kristoffer.carlsson proposed to do use it. So now we are back to square 1 - see the original question above.

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [January 4, 2017, 1:02pm UTC](https://discourse.julialang.org/t/invoke-a-method/1275/6 "2017-01-04T13:02:17Z")

</div>

Ok, I missunderstood your question then.

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [January 4, 2017, 1:09pm UTC](https://discourse.julialang.org/t/invoke-a-method/1275/7 "2017-01-04T13:09:00Z")

</div>

I think the answer then is: “not really”.

Maybe this hack works for you:

```julia
julia> stripT(T) = Tuple{T.parameters[2:end]...}

julia> invoke(getfield(m.module, m.name), stripT(m.sig), 1,4)
5

```

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [January 4, 2017, 5:25pm UTC](https://discourse.julialang.org/t/invoke-a-method/1275/8 "2017-01-04T17:25:44Z")

</div>

I’ve proposed a number of times making methods more first class – being able to directly call invoke on a method object would be a significant part of that. If you’re feeling like making a PR to Julia, this would be a good one. If not, you could open an issue requesting such a feature.

---

<div class="post-metadata">

**Author:** ![jameson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jameson/32/23_2.png) [@jameson](https://discourse.julialang.org/u/jameson)\
**Post date:** [January 4, 2017, 6:48pm UTC](https://discourse.julialang.org/t/invoke-a-method/1275/9 "2017-01-04T18:48:21Z")

</div>

See also [Proposal for a first-class dispatch wrapper](https://discourse.julialang.org/t/proposal-for-a-first-class-dispatch-wrapper/1127) for a related proposal.

---

<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:** [January 5, 2017, 9:22am UTC](https://discourse.julialang.org/t/invoke-a-method/1275/10 "2017-01-05T09:22:07Z")

</div>

@StefanKarpinski @jameson Thanks for the pointers. While I don’t doubt that first-class methods would be useful in some applications, in my particular use case I decided that they would be a very convoluted and unidiomatic solution.

What I wanted to do is basically the following: have a `DataFrame`-like object which is basically a `Dict{Symbol,Vector}`. I was looking for a way to map vectors using user provided functions, and looking up the vectors dynamically. For example, suppose the user supplies a function `f(b,a)`. The map function would look up `:a` and `:b` in the `Dict`, assign them to positions `2` and `1`, and call broadcast with `f` and the vectors in the right place. I asked about [argument name extraction](https://discourse.julialang.org/t/extract-argument-names/862/2) here. The inspiration was [Mamba’s implementation](https://github.com/brian-j-smith/Mamba.jl/blob/master/src/utils.jl#L15) of this.

I am currently of the opinion that this is unnatural in Julia, since each function can have many methods, so I could have multiple matches. I could of course apply some kind of dispatch machinery to select the “most specific” one based on the type signature, but in the end I concluded that this is confusing and error-prone, and the [DataFramesMeta.@with](https://github.com/JuliaStats/DataFramesMeta.jl/blob/master/src/DataFramesMeta.jl#L20) is a much more natural solution.

---

<div class="post-metadata">

**Author:** ![schlichtanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/schlichtanders/32/32145_2.png) [@schlichtanders](https://discourse.julialang.org/u/schlichtanders)\
**Post date:** [November 15, 2018, 8:03am UTC](https://discourse.julialang.org/t/invoke-a-method/1275/11 "2018-11-15T08:03:23Z")

</div>

has something happened around invoking a method in the meanwhile?  
maybe with julia 1.0?

when having a method object, it would really be awesome if one could just call it

---

<div class="post-metadata">

**Author:** ![tecosaur](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tecosaur/32/23206_2.png) [@tecosaur](https://discourse.julialang.org/u/tecosaur)\
**Post date:** [September 27, 2024, 9:42am UTC](https://discourse.julialang.org/t/invoke-a-method/1275/12 "2024-09-27T09:42:18Z")

</div>

I’ve recently run into this myself, and I thought I’d mention a hacky workaround that I’m currently contemplating.

```julia
function (m::Method)(args...)
    fntype, argtypes... = m.sig.types
    invoke(fntype.instance, Tuple{argtypes...}, args...)
end

```

How inferrable is this? Not at all 😅
