# Function type specification

**URL:** <https://discourse.julialang.org/t/function-type-specification/1749>\
**Category:** Internals & Design\
**Created:** [January 29, 2017, 5:37am UTC](https://discourse.julialang.org/t/function-type-specification/1749 "2017-01-29T05:37:18Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![wsshin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wsshin/32/360_2.png) [@wsshin](https://discourse.julialang.org/u/wsshin)\
**Post date:** [January 29, 2017, 5:37am UTC](https://discourse.julialang.org/t/function-type-specification/1749/1 "2017-01-29T05:37:18Z")

</div>

Consider two simple functions that take one `Float64` and return another `Float64`:

```julia
julia> f(x) = x^2
f (generic function with 1 method)

julia> g(x) = x^3
g (generic function with 1 method)

```

Also, consider a higher-order function that takes a function and returns another function:

```julia
julia> h(fun::Function) = x -> 2fun(x)
h (generic function with 1 method)

julia> h(f)(2.)
8.0

julia> h(g)(2.)
16.0

```

Now, I want to apply `h` to an array of functions. However, such operation’s type stability depends on whether the array is homogeneous or not:

```julia
julia> fun_homog = [g, g]
2-element Array{#g,1}:
 g
 g

julia> fun_inhomog = [g, h]
2-element Array{Function,1}:
 g
 h

julia> @code_warntype broadcast(h, fun_homog)
(No instability)

julia> @code_warntype broadcast(h, fun_inhomog)
(Lots of instability)

```

The point here is that `fun_homog`’s element type is `#g`, whereas `fun_inhomog`’s element type is abstract `Function`.

I wonder if this kind of instability can be eliminated by allowing function type specification (like C’s function pointer). Clearly, `f` and `g` here are of the same type, in the sense that both take one `Float64` and return another `Float64`. However, currently once an array `[f, g]` is formed, the array does not recognize such similarity between `f` and `g`.

In other words, I wonder if the array `[f, g]` can be typed as something like `Array[::Function(::Float64)::Float64]` to indicate that the array’s elements are functions that take one `Float64` and return another `Float64`, and also wonder if such information could be used to eliminate the aforementioned type instability.

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [January 29, 2017, 6:02am UTC](https://discourse.julialang.org/t/function-type-specification/1749/2 "2017-01-29T06:02:01Z")

</div>

[https://github.com/yuyichao/FunctionWrappers.jl](https://github.com/yuyichao/FunctionWrappers.jl)

---

<div class="post-metadata">

**Author:** ![wsshin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wsshin/32/360_2.png) [@wsshin](https://discourse.julialang.org/u/wsshin)\
**Post date:** [January 29, 2017, 9:07pm UTC](https://discourse.julialang.org/t/function-type-specification/1749/3 "2017-01-29T21:07:01Z")

</div>

Glad to know that there is already an effort in this direction!

How can I use this in the above case? Could you show an example?

---

<div class="post-metadata">

**Author:** ![purplishrock](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/purplishrock/32/13451_2.png) [@purplishrock](https://discourse.julialang.org/u/purplishrock)\
**Post date:** [January 30, 2017, 1:22am UTC](https://discourse.julialang.org/t/function-type-specification/1749/4 "2017-01-30T01:22:31Z")

</div>

Why wasn’t function typing part of Julia’s design from the start ?

I find it _really_ surprising that something like

::Function(::Float64)::Float64

doesn’t already exist.

Isn’t functionwrappers a hack to get around what is really a design problem ?

---

<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:** [January 30, 2017, 2:26am UTC](https://discourse.julialang.org/t/function-type-specification/1749/5 "2017-01-30T02:26:23Z")

</div>

> [@purplishrock](#):
>
> Why wasn’t function typing part of Julia’s design from the start ?

Because it has problematic interactions with multiple dispatch: [https://github.com/JeffBezanson/phdthesis/blob/876be73a5aab9b034fac3eb9ea9d8f96713f786f/chap4.tex](https://github.com/JeffBezanson/phdthesis/blob/876be73a5aab9b034fac3eb9ea9d8f96713f786f/chap4.tex)

---

<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:** [January 31, 2017, 4:37pm UTC](https://discourse.julialang.org/t/function-type-specification/1749/6 "2017-01-31T16:37:41Z")

</div>

The biggest challenge right now to doing something like this is that the type of those functions isn’t `::Function(::Float64)::Float64`, they are `::Function(f, ::Float64)::Float64` and `::Function(g, ::Float64)::Float64`. For another recent post on the topic, see [Proposal for a first-class dispatch wrapper - #2 by jameson](https://discourse.julialang.org/t/proposal-for-a-first-class-dispatch-wrapper/1127/2).

---

<div class="post-metadata">

**Author:** ![felix](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/felix/32/604_2.png) [@felix](https://discourse.julialang.org/u/felix)\
**Post date:** [February 1, 2017, 8:05am UTC](https://discourse.julialang.org/t/function-type-specification/1749/7 "2017-02-01T08:05:50Z")

</div>

That proposal looked really nice, are the plans in this direction?

---

<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:** [February 1, 2017, 2:34pm UTC](https://discourse.julialang.org/t/function-type-specification/1749/8 "2017-02-01T14:34:41Z")

</div>

Seems plausible, but the devil’s in the details and actually implementing it. Fortunately @jameson has a solid track record of delivering on even harder projects than this 🤓

That said, this is a complex issue and we’ll have to think it through pretty carefully.
