# Creating a function from a subset of the methods of another function

**URL:** <https://discourse.julialang.org/t/creating-a-function-from-a-subset-of-the-methods-of-another-function/32403>\
**Category:** General Usage\
**Tags:** dispatch\
**Created:** [December 17, 2019, 7:45pm UTC](https://discourse.julialang.org/t/creating-a-function-from-a-subset-of-the-methods-of-another-function/32403 "2019-12-17T19:45:34Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![longemen3000](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/longemen3000/32/7298_2.png) [@longemen3000](https://discourse.julialang.org/u/longemen3000)\
**Post date:** [December 17, 2019, 7:45pm UTC](https://discourse.julialang.org/t/creating-a-function-from-a-subset-of-the-methods-of-another-function/32403/1 "2019-12-17T19:45:34Z")

</div>

lets say i have a function `f` with a lot methods:

```julia
julia>methods(f) #invented output
[1] f(x::Int64, y::Int64) in PackageA at \.julia\packages\PackageA\yPcDQ\src\functions.jl:43
[2] f(x::Float64, y::Float64) in Main at REPL[50]:1
[3] f(x::String,y::String) in Base at somefile.jl:456
...
[550] f(x::AwesomeType, y::AwesomeType) in AwesomeTypes at \.julia\packages\AwesomeTypes \asSdS\src\awesome.jl:43

```

I want a function `f2` defined as a subset of the methods of `f` from a subset of packages (for example, just `Base`, `PackageX1`, and `PackageX2`). for example, with the criteria above, the function `f2(AwesomeType(1),AwesomeType(2))` should throw an error, but `f2(1.0,1.0)` should be ok.

There is any way to do this?

---

<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:** [December 17, 2019, 7:53pm UTC](https://discourse.julialang.org/t/creating-a-function-from-a-subset-of-the-methods-of-another-function/32403/2 "2019-12-17T19:53:01Z")

</div>

```julia
f2(signature...) = f(...)

```

Repeat for as many subsignatures as necessary.

---

<div class="post-metadata">

**Author:** ![longemen3000](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/longemen3000/32/7298_2.png) [@longemen3000](https://discourse.julialang.org/u/longemen3000)\
**Post date:** [December 17, 2019, 8:06pm UTC](https://discourse.julialang.org/t/creating-a-function-from-a-subset-of-the-methods-of-another-function/32403/3 "2019-12-17T20:06:45Z")

</div>

how can i extract the signature of `f` from the list of his methods?, the solution is trivial with 20 methods, but if i have 500 of them, writing by hand is infeasible, being more specific, i want to do that function programmatically, using `f` as base and a Package criteria.  
For example, this pseudocode:

```julia
macro method_subset(fun,packages) 
f2 = gensym("subset_function") 

quote
  fmethods = vector_of_methods($fun) 
  for i in 1:length(fmethods)
    if from_package(fmethods[i]) in packages
      $f2(signature(fmethods[i])...) = $fun(signature(fmethods[i])...) 
    end
  end
end

f2 = @method_subset f (Base, Package4, Package1)

```

---

<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:** [December 17, 2019, 8:09pm UTC](https://discourse.julialang.org/t/creating-a-function-from-a-subset-of-the-methods-of-another-function/32403/4 "2019-12-17T20:09:47Z")

</div>

You can call `methods` on it, but why do you have to? How do you want to express at a high level which signatures you want to “select”? Whatever that selection expression is, already is a single method signature, so just use that when defining `f2`.

---

<div class="post-metadata">

**Author:** ![longemen3000](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/longemen3000/32/7298_2.png) [@longemen3000](https://discourse.julialang.org/u/longemen3000)\
**Post date:** [December 17, 2019, 8:20pm UTC](https://discourse.julialang.org/t/creating-a-function-from-a-subset-of-the-methods-of-another-function/32403/5 "2019-12-17T20:20:29Z")

</div>

sorry, i got the pseudo code wrong. putting another (more specific) example. if `f = cos` and the subset is just the package `Base`, so something like this:

```julia
using Measurements
cos2 = @method_subset cos (Base)
cos2(1.0 ± 0.1) #not defined

```

---

<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:** [December 18, 2019, 5:57pm UTC](https://discourse.julialang.org/t/creating-a-function-from-a-subset-of-the-methods-of-another-function/32403/6 "2019-12-18T17:57:13Z")

</div>

Can you not do

```julia
cos2(args...) = cos(args...)
# add other methods

```

If you want, this function can even be called `cos`, you just have to qualify the other call as `Base.cos`.

---

<div class="post-metadata">

**Author:** ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)\
**Post date:** [December 18, 2019, 6:06pm UTC](https://discourse.julialang.org/t/creating-a-function-from-a-subset-of-the-methods-of-another-function/32403/7 "2019-12-18T18:06:46Z")

</div>

They’re saying that they want a function which only has the methods of `cos` that came from `Base` but not (for example) Measurements.jl.

---

<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:** [December 18, 2019, 6:10pm UTC](https://discourse.julialang.org/t/creating-a-function-from-a-subset-of-the-methods-of-another-function/32403/8 "2019-12-18T18:10:32Z")

</div>

Ah, I see. Seems like something like the pseudo code would be necessary then since it’s not a type-level restriction, it’s a question of where the methods were defined.
