# Composition of functions and method dispatch

**URL:** <https://discourse.julialang.org/t/composition-of-functions-and-method-dispatch/35189>\
**Category:** General Usage\
**Tags:** question, composition\
**Created:** [February 26, 2020, 6:45pm UTC](https://discourse.julialang.org/t/composition-of-functions-and-method-dispatch/35189 "2020-02-26T18:45:17Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)\
**Post date:** [February 26, 2020, 6:45pm UTC](https://discourse.julialang.org/t/composition-of-functions-and-method-dispatch/35189/1 "2020-02-26T18:45:18Z")

</div>

I have a number of functions like so

```julia
gfun(m::Type1,t) 
gfun(m::Type2,t)
gfun(m::Type3,x,t)

```

In every case, I would like to define a different function, call it `hfun`, that takes the same arguments and just composes a simple function, maybe `exp`, with the appropriate `gfun` (based on the arguments supplied).

```julia
hfun(m::Type1,t) = exp(gfun(m::Type1,t) )
hfun(m::Type2,t) = exp(gfun(m::Type2,t) )
hfun(m::Type3,x,t) = exp(gfun(m::Type3,x,t))

```

How do I define a “generic” `hfun` one time, rather than do it as I have written it?

---

<div class="post-metadata">

**Author:** ![pbayer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pbayer/32/11675_2.png) [@pbayer](https://discourse.julialang.org/u/pbayer)\
**Post date:** [February 26, 2020, 6:53pm UTC](https://discourse.julialang.org/t/composition-of-functions-and-method-dispatch/35189/2 "2020-02-26T18:53:28Z")

</div>

```julia
hfun(m::T, t) where {T<:Union{Type1,Type2,Type2}} = exp(gfun(m,t))
hfun(m:Type3,x,t) = exp(gfun(m,x,t))

```

Yet more elegant: if you have super type for `Type1,Type2...`, then you can define

```julia
hfun(m::T, t) where {T<:MySuperType} = exp(gfun(m,t))

```

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [February 26, 2020, 6:54pm UTC](https://discourse.julialang.org/t/composition-of-functions-and-method-dispatch/35189/3 "2020-02-26T18:54:21Z")

</div>

How about:

```julia
hfun(args....) = exp(gfun(args...))

```

?

---

<div class="post-metadata">

**Author:** ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)\
**Post date:** [February 26, 2020, 6:54pm UTC](https://discourse.julialang.org/t/composition-of-functions-and-method-dispatch/35189/4 "2020-02-26T18:54:55Z")

</div>

`m` has a supertype but note that the number of arguments changes based on its type as well. That is really what is tripping me up.

---

<div class="post-metadata">

**Author:** ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)\
**Post date:** [February 26, 2020, 7:03pm UTC](https://discourse.julialang.org/t/composition-of-functions-and-method-dispatch/35189/5 "2020-02-26T19:03:03Z")

</div>

Aha. I guess I didn’t think about splatting _all_ of the function arguments. I will give that a shot!

---

<div class="post-metadata">

**Author:** ![DrChainsaw](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/drchainsaw/32/8497_2.png) [@DrChainsaw](https://discourse.julialang.org/u/DrChainsaw)\
**Post date:** [February 26, 2020, 7:59pm UTC](https://discourse.julialang.org/t/composition-of-functions-and-method-dispatch/35189/6 "2020-02-26T19:59:21Z")

</div>

The MWE can also be handled with the composition operator:

```julia
hfun = exp ∘ gfun

```

In this case gfun will be a variable and not a function which may or may not make a difference in your case.

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [February 26, 2020, 8:08pm UTC](https://discourse.julialang.org/t/composition-of-functions-and-method-dispatch/35189/7 "2020-02-26T20:08:53Z")

</div>

BTW, doing `const hfun = ...` will give better performance if `hfun` is a global variable.
