# Dynamically invoke all the methods of a function

**URL:** <https://discourse.julialang.org/t/dynamically-invoke-all-the-methods-of-a-function/38258>\
**Category:** General Usage\
**Created:** [April 26, 2020, 7:35pm UTC](https://discourse.julialang.org/t/dynamically-invoke-all-the-methods-of-a-function/38258 "2020-04-26T19:35:53Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![essenciary](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/essenciary/32/210469_2.png) [@essenciary](https://discourse.julialang.org/u/essenciary)\
**Post date:** [April 26, 2020, 7:35pm UTC](https://discourse.julialang.org/t/dynamically-invoke-all-the-methods-of-a-function/38258/1 "2020-04-26T19:35:53Z")

</div>

I want to write a function `A.x()` which in its body automatically invokes all the available methods of `A.x()`. However, I can’t find the reflection mechanism that allows me to do this. I was hoping `methods` will do it, but it doesn’t return functions, ex:

```julia
julia> [r() for r in methods(rand)]
ERROR: MethodError: objects of type Method are not callable

```

Also, I would like to get the Module in which the method was defined.

Is it possible? Thanks

---

<div class="post-metadata">

**Author:** ![tk3369](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tk3369/32/2824_2.png) [@tk3369](https://discourse.julialang.org/u/tk3369)\
**Post date:** [April 26, 2020, 8:14pm UTC](https://discourse.julialang.org/t/dynamically-invoke-all-the-methods-of-a-function/38258/2 "2020-04-26T20:14:03Z")

</div>

Every method may take different arguments though… Does your use case only need to handle calling it without any arguments?

The `Method` object contains a `module` field e.g.

```julia
julia> struct Foo end

julia> using Random

julia> Random.rand(::Foo) = 1

julia> unique([m.module for m in methods(rand)])
2-element Array{Module,1}:
 Random
 Main

```

---

<div class="post-metadata">

**Author:** ![essenciary](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/essenciary/32/210469_2.png) [@essenciary](https://discourse.julialang.org/u/essenciary)\
**Post date:** [April 26, 2020, 8:32pm UTC](https://discourse.julialang.org/t/dynamically-invoke-all-the-methods-of-a-function/38258/3 "2020-04-26T20:32:38Z")

</div>

Thank you - the methods are part of an interface so I’m thinking of specialising them on something like `Val{Symbol(@ __MODULE__ )}` so then I can invoke them using the module’s name.

The module field is great - but how can I get the function from the method object and how do I invoke it?

---

<div class="post-metadata">

**Author:** ![cstjean](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cstjean/32/1444_2.png) [@cstjean](https://discourse.julialang.org/u/cstjean)\
**Post date:** [April 26, 2020, 9:02pm UTC](https://discourse.julialang.org/t/dynamically-invoke-all-the-methods-of-a-function/38258/4 "2020-04-26T21:02:55Z")

</div>

[GitHub - cstjean/ConferenceCall.jl](https://github.com/cstjean/ConferenceCall.jl) ?

---

<div class="post-metadata">

**Author:** ![tk3369](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tk3369/32/2824_2.png) [@tk3369](https://discourse.julialang.org/u/tk3369)\
**Post date:** [April 26, 2020, 10:09pm UTC](https://discourse.julialang.org/t/dynamically-invoke-all-the-methods-of-a-function/38258/5 "2020-04-26T22:09:23Z")

</div>

> [@essenciary](#):
>
> The module field is great - but how can I get the function from the method object and how do I invoke it?

I thought you already have a reference to the function to begin with? 🤔 Invoking the function is just simply calling it with the right arguments.

I guess it can become tricky to make it dispatch to the exact method. For example, if you have two methods - one taking `Int` and another taking `Number`, then you have to figured out how to call the second method with a non-`Int` `Number`. Maybe your use case is simpler than what I am imagining…

---

<div class="post-metadata">

**Author:** ![essenciary](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/essenciary/32/210469_2.png) [@essenciary](https://discourse.julialang.org/u/essenciary)\
**Post date:** [April 27, 2020, 6:54am UTC](https://discourse.julialang.org/t/dynamically-invoke-all-the-methods-of-a-function/38258/6 "2020-04-27T06:54:17Z")

</div>

> [@tk3369](#):
>
> I thought you already have a reference to the function to begin with?

I was looking to get a reference to the function within the methods iteration. But you’re right, given that I already have the function’s name and I get the module within the iteration, I can just invoke it “by hand”.

Yes, it’s a simpler situation in terms of arguments as these are for an interface, so I know what argument they will take _by convention_.

However, this is becoming complicated enough and dubious enough to look like a code smell. So I refactored my code so that each module, instead of extending the main function, pushes its function into an array in the main package. The code now just iterates over the functions pushed in the array which is a pretty common Julian design pattern.

Thanks for your help!

---

<div class="post-metadata">

**Author:** ![essenciary](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/essenciary/32/210469_2.png) [@essenciary](https://discourse.julialang.org/u/essenciary)\
**Post date:** [April 27, 2020, 6:59am UTC](https://discourse.julialang.org/t/dynamically-invoke-all-the-methods-of-a-function/38258/7 "2020-04-27T06:59:41Z")

</div>

> [@cstjean](#):
>
> [https://github.com/cstjean/ConferenceCall.jl](https://github.com/cstjean/ConferenceCall.jl) ?

This is beautiful, thank you! It’s exactly how I defined the functions, using a `Val` based on the module’s name as the convention for the interface. But this is much cleaner and less error prone, with the automatically generated signature.

---

<div class="post-metadata">

**Author:** ![cstjean](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cstjean/32/1444_2.png) [@cstjean](https://discourse.julialang.org/u/cstjean)\
**Post date:** [April 27, 2020, 1:44pm UTC](https://discourse.julialang.org/t/dynamically-invoke-all-the-methods-of-a-function/38258/8 "2020-04-27T13:44:15Z")

</div>

> [@essenciary](#):
>
> So I refactored my code so that each module, instead of extending the main function, pushes its function into an array in the main package.

I considered that solution for my use cases too, but `push!(OtherPackage.array, my_function)` at the top-level doesn’t work with precompilation. It has to be in the ` __init__ `, which is inconvenient.

---

<div class="post-metadata">

**Author:** ![essenciary](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/essenciary/32/210469_2.png) [@essenciary](https://discourse.julialang.org/u/essenciary)\
**Post date:** [April 27, 2020, 3:35pm UTC](https://discourse.julialang.org/t/dynamically-invoke-all-the-methods-of-a-function/38258/9 "2020-04-27T15:35:10Z")

</div>

Yes, I figured that out myself. Functions were not being added to the array but no warning or anything… So I just tried in ` __init__ ` and was happy to see that it works. I’m OK with this as it’s for developers wanting to write plugins to interface with the package.
