# Ability to define function templates

**URL:** <https://discourse.julialang.org/t/ability-to-define-function-templates/4867>\
**Category:** Internals & Design\
**Created:** [July 14, 2017, 10:53pm UTC](https://discourse.julialang.org/t/ability-to-define-function-templates/4867 "2017-07-14T22:53:06Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![azraq27](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/azraq27/32/951_2.png) [@azraq27](https://discourse.julialang.org/u/azraq27)\
**Post date:** [July 14, 2017, 10:53pm UTC](https://discourse.julialang.org/t/ability-to-define-function-templates/4867/1 "2017-07-14T22:53:06Z")

</div>

I’ve ran into multiple scenarios now where I’d like to pass a function as an argument to another function, but I want to make sure it conforms to a certain type signature.

For example:

```
function dosomething(times::Int,doit::Function)
    for i=1:times
         doit(i)
    end
end

```

What I’d like to do in the above example is restrict `doit()` to any `Function` that takes a single `Int` argument. Obviously, I could just document that in the usage, and “hope” that it works out. But, I think it would be cleaner to be able to specify the type signature I require somehow.

I can’t see a straightforward way to do this. Am I missing something? Would this be a useful feature to add to the language? I’ve enjoyed a similar feature, “`Protocols`” in Objective-C.

Thanks.

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [July 14, 2017, 11:00pm UTC](https://discourse.julialang.org/t/ability-to-define-function-templates/4867/2 "2017-07-14T23:00:01Z")

</div>

> [@azraq27](#):
>
> What I’d like to do in the above example is restrict doit() to any Function that takes a single Int argument.

That’s tough because functions don’t take in arguments. Methods have a well-defined number of arguments. And what you’re looking for then is a Method which works with an `Int`, which can be `f(x)`, i.e. no types mentioned could be the one that works great on an Int.

To add to the complications here, not every function subtypes `Function` (which is just an abstract type which any function defined by the `function f(x) ...` will subtype, so not every function!).

> [@azraq27](#):
>
> Would this be a useful feature to add to the language?

Something like this could be interesting, but it would need to take multiple-dispatch seriously.

---

<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:** [July 15, 2017, 6:48am UTC](https://discourse.julialang.org/t/ability-to-define-function-templates/4867/3 "2017-07-15T06:48:23Z")

</div>

> [@azraq27](#):
>
> Obviously, I could just document that in the usage, and “hope” that it works out.

Do that. If you could require a signature for the function, you would get a `MethodError` when calling `dosomething`. Since you can’t, you will get a `MethodError` when calling `doit`. I don’t see this as much of a problem.

In dynamic languages with multiple dispatch, it is OK to only restrict types of arguments when you want different behavior based on them, not to preclude the possibility of a `MethodError` later. You get one anyway, just at a different point, which is OK.

---

<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:** [July 15, 2017, 3:07pm UTC](https://discourse.julialang.org/t/ability-to-define-function-templates/4867/4 "2017-07-15T15:07:20Z")

</div>

It’s not ideal, but you can `@assert applicable(doit, (Int,)) "doit must accept an Int argument"`

---

<div class="post-metadata">

**Author:** ![leiteiro](https://avatars.discourse-cdn.com/v4/letter/l/b5e925/32.png) [@leiteiro](https://discourse.julialang.org/u/leiteiro)\
**Post date:** [July 15, 2017, 4:05pm UTC](https://discourse.julialang.org/t/ability-to-define-function-templates/4867/5 "2017-07-15T16:05:22Z")

</div>

Recently I had a similar question: [Classification of functions](https://discourse.julialang.org/t/classification-of-functions/4468).

Something like “protocols” in Objective-C would be a great thing.  
Perhaps add it to the [whishlist](https://discourse.julialang.org/t/list-of-most-desired-features-for-julia-v1-x/4481)?

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [July 15, 2017, 6:54pm UTC](https://discourse.julialang.org/t/ability-to-define-function-templates/4867/6 "2017-07-15T18:54:36Z")

</div>

> [@azraq27](#):
>
> where I’d like to pass a function as an argument to another function, but I want to make sure it conforms to a certain type signature.

See [crazy idea: dispatch on return type · Issue #19206 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/19206) and the links therein.

---

<div class="post-metadata">

**Author:** ![jeff.bezanson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff.bezanson/32/48_2.png) [@jeff.bezanson](https://discourse.julialang.org/u/jeff.bezanson)\
**Post date:** [July 18, 2017, 3:09pm UTC](https://discourse.julialang.org/t/ability-to-define-function-templates/4867/7 "2017-07-18T15:09:29Z")

</div>

See also [https://github.com/JuliaLang/julia/issues/210](https://github.com/JuliaLang/julia/issues/210), a rather long-standing issue! We don’t have this feature yet, but it’s interesting to think about how it might work.

Also, as a minor aside, in typical terminology this is not called a template, but a function type or arrow type.

---

<div class="post-metadata">

**Author:** ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)\
**Post date:** [July 18, 2017, 5:31pm UTC](https://discourse.julialang.org/t/ability-to-define-function-templates/4867/8 "2017-07-18T17:31:45Z")

</div>

That just replaces MethodError with AssertionError.

---

<div class="post-metadata">

**Author:** ![azraq27](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/azraq27/32/951_2.png) [@azraq27](https://discourse.julialang.org/u/azraq27)\
**Post date:** [July 20, 2017, 3:25am UTC](https://discourse.julialang.org/t/ability-to-define-function-templates/4867/9 "2017-07-20T03:25:20Z")

</div>

I guess now that I think through it, I can’t see why there’s an advantage to having the parameters be specified initially, rather than throwing an error later when the function is called. Just seems sloppy – although I can’t think of a functional difference that would motivate adding the feature.

As it stands, it seems simply calling the function, and raising a MethodError, is the most straightforward solution. Along with providing appropriate documentation of the arguments.
