# Functions and callable methods

**URL:** https://discourse.julialang.org/t/functions-and-callable-methods/2983
**Category:** General Usage
**Tags:** question
**Created:** [March 31, 2017, 12:40pm UTC](https://discourse.julialang.org/t/functions-and-callable-methods/2983 "2017-03-31T12:40:56Z")
**Posts on this page:** 3
**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: [March 31, 2017, 12:40pm UTC](https://discourse.julialang.org/t/functions-and-callable-methods/2983/1 "2017-03-31T12:40:56Z")

</div>

I am confused about how to organize my code for objects which behave like functions. Some functions do different things when called with arguments which are `<: Function`, eg `Plots.plot` will plot the function, but will not plot a callable object which is not `<: Function` (because it can’t dispatch on it, unless a recipe is defined etc).

Generally, a type `T`

1. can have `(f::T)(...)` methods defined, and
2. `T <: Function` can hold.

Apparently these two are orthogonal, one can have a `<: Function` without it being callable, and vice versa. I am wondering if it is good style to impose both at the same time, or if there are good arguments for doing one but not the other.

Sorry if the question is vague. I am assuming that others have ran into this and would like to hear what you think.

---

<div class="post-metadata">

### Author: ![Evizero](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/evizero/32/10118_2.png) [@Evizero](https://discourse.julialang.org/u/Evizero)
#### Post date: [March 31, 2017, 1:03pm UTC](https://discourse.julialang.org/t/functions-and-callable-methods/2983/2 "2017-03-31T13:03:23Z")

</div>

I have also run into this a couple of times when writing functions that accept functions.

In general I try to avoid dispatching on `::Function` to allow passing callable objects, but sometimes that is quite difficult to pull off. There is this convention of putting optional functions as the first argument to allow for the `f(...) do ... end` syntax. But especially a flexible first argument type can easily cause ambiguities

---

<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: [April 7, 2017, 5:08pm UTC](https://discourse.julialang.org/t/functions-and-callable-methods/2983/3 "2017-04-07T17:08:47Z")

</div>

> [@Tamas\_Papp](#):
>
> Apparently these two are orthogonal

@Evizero is correct that it’s often best to avoid restricting the dispatch. This advice also generally applies to more than just Functions.

To further his point, I would suggest that making a subtype of `Function` can be more generally be treated as a trait property of the type. Thus it’s not inherently meaningful (as you noted, being actually callable is an orthogonal property – and having an applicable method is also entirely different). But as a trait, it is useful for informing dispatch that it is intended for this object to be treated as a Callable. Does that make sense?
