# Type inference for a singleton of type Function

**URL:** <https://discourse.julialang.org/t/type-inference-for-a-singleton-of-type-function/46544>\
**Category:** New to Julia\
**Created:** [September 13, 2020, 11:17am UTC](https://discourse.julialang.org/t/type-inference-for-a-singleton-of-type-function/46544 "2020-09-13T11:17:58Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Adam\_Haber](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adam_haber/32/9242_2.png) [@Adam\_Haber](https://discourse.julialang.org/u/Adam_Haber)\
**Post date:** [September 13, 2020, 11:17am UTC](https://discourse.julialang.org/t/type-inference-for-a-singleton-of-type-function/46544/1 "2020-09-13T11:17:58Z")

</div>

I have an object that stores (amongst other things) a vector of functions. When I use multiple functions, everything works nicely. However, if I pass it a vector with a single function, I’m getting type-inference problems.

Here’s a simplified example:

```nohighlight
function times2(i)
    2i
end

function times_n(i,n)
    i*n
end

```

This works:

```nohighlight
[times2, i -> times_n(i,8)]

```

And gives

```julia
2-element Array{Function,1}:
 times2
 var"#136#137"()

```

Which is what I want (would’ve preferred a more informative name but that’s just a matter of convenience).  
However, instead if I try:

```nohighlight
l = [times2]
push!(l, i -> times_n(i,8))

```

I get an error:

```julia
MethodError: Cannot `convert` an object of type var"#168#169" to an object of type typeof(times2)
Closest candidates are:
  convert(::Type{T}, !Matched::T) where T at essentials.jl:171

Stacktrace:
 [1] push!(::Array{typeof(times2),1}, ::Function) at ./array.jl:913
 [2] top-level scope at In[152]:2

```

This also happens if I initialise `l` with the anonymous function and then try to add `times2`.

I’ve tried annotating the relevant expressions with `::Function` but I still get that `l` is a list of objects of type `typeof(times2)`.

I apologise if this is a trivial question - I’ve tried to search for an answer on the forum but couldn’t find any. Any help would be much appreciated!

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [September 13, 2020, 11:37am UTC](https://discourse.julialang.org/t/type-inference-for-a-singleton-of-type-function/46544/2 "2020-09-13T11:37:09Z")

</div>

I don’t know why your example doesn’t work, but maybe this is an alternative to your code:

```julia
l = [times2]
l = vcat(l,[i -> times_n(i,8)])

```

---

<div class="post-metadata">

**Author:** ![tomerarnon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomerarnon/32/3170_2.png) [@tomerarnon](https://discourse.julialang.org/u/tomerarnon)\
**Post date:** [September 13, 2020, 11:41am UTC](https://discourse.julialang.org/t/type-inference-for-a-singleton-of-type-function/46544/3 "2020-09-13T11:41:12Z")

</div>

The reason you cannot push to the `[times2]` vector is this:

```julia
julia> typeof([times2])
Array{typeof(times2),1}

```

The array created with `[times2]` is specifically an array that is allowed to contain objects of type `typeof(times2)`. This is in much the same way that an array of Ints may only store Ints and initializing `[15]` will create such a vector (you cannot then push `1.6` to it).

This works just fine:

```julia
julia> v = [times2]
1-element Array{typeof(times2),1}:
 times2 (generic function with 1 method)

julia> push!(v, times2)
2-element Array{typeof(times2),1}:
 times2 (generic function with 1 method)
 times2 (generic function with 1 method)

```

If you want a vector that can store generic Functions, you can declare it like so:

```julia
julia> v = Function[times2]
1-element Array{Function,1}:
 times2 (generic function with 1 method)

julia> push!(v, times_n)
2-element Array{Function,1}:
 times2 (generic function with 1 method)
 times_n (generic function with 1 method)

```

---

<div class="post-metadata">

**Author:** ![danielw2904](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danielw2904/32/10890_2.png) [@danielw2904](https://discourse.julialang.org/u/danielw2904)\
**Post date:** [September 13, 2020, 11:41am UTC](https://discourse.julialang.org/t/type-inference-for-a-singleton-of-type-function/46544/4 "2020-09-13T11:41:48Z")

</div>

Did you try

```julia
l = Function[times2]

```

?  
Edit:  
One min too late. What @tomerarnon said!

---

<div class="post-metadata">

**Author:** ![Adam\_Haber](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adam_haber/32/9242_2.png) [@Adam\_Haber](https://discourse.julialang.org/u/Adam_Haber)\
**Post date:** [September 13, 2020, 11:59am UTC](https://discourse.julialang.org/t/type-inference-for-a-singleton-of-type-function/46544/5 "2020-09-13T11:59:53Z")

</div>

Thanks, this was exactly what I needed!

> [@tomerarnon](#):
>
> The array created with `[times2]` is specifically an array that is allowed to contain objects of type `typeof(times2)` . This is in much the same way that an array of Ints may only store Ints and initializing `[15]` will create such a vector (you cannot then push `1.6` to it).

Just out of curiosity - if I’ve just defined `times2` as a _function_, why can’t Julia infer that its type is `Function`? And why doesn’t type annotation help?

---

<div class="post-metadata">

**Author:** ![danielw2904](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danielw2904/32/10890_2.png) [@danielw2904](https://discourse.julialang.org/u/danielw2904)\
**Post date:** [September 13, 2020, 12:26pm UTC](https://discourse.julialang.org/t/type-inference-for-a-singleton-of-type-function/46544/6 "2020-09-13T12:26:27Z")

</div>

I think that has to do with multiple dispatch. When you create a function a struct with that function is created and the different methods are then instances of this struct.  
So

```julia
times2(i::Int)
times2(f::Float64)

```

Are both of `type` `times2`

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [September 13, 2020, 12:43pm UTC](https://discourse.julialang.org/t/type-inference-for-a-singleton-of-type-function/46544/7 "2020-09-13T12:43:01Z")

</div>

A more deep reasoning can be found here:

> [@What is typeof(fun)?](https://discourse.julialang.org/t/what-is-typeof-fun/33523):
>
> when I methods(Base.map), I see something like: [1] map(::typeof(zero), A::BitArray) in Base at bitarray.jl:1659 what is typeof(zero)? indeed, I found that: julia\> f(x) = 2x f (generic function with 1 method) julia\> typeof(f) typeof(f) julia\> typeof(f) \<: Function true julia\> typeof(f) isa DataType true julia\> f isa Function true it’s quite strange to me: why typeof(f) not return Function? official docs mentions nothing about it.

---

<div class="post-metadata">

**Author:** ![tomerarnon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomerarnon/32/3170_2.png) [@tomerarnon](https://discourse.julialang.org/u/tomerarnon)\
**Post date:** [September 13, 2020, 12:45pm UTC](https://discourse.julialang.org/t/type-inference-for-a-singleton-of-type-function/46544/8 "2020-09-13T12:45:51Z")

</div>

> [@Adam\_Haber](#):
>
> Just out of curiosity - if I’ve just defined `times2` as a _function_ , why can’t Julia infer that its type is `Function` ? And why doesn’t type annotation help?

It does, but `Function` is an abstract type, and julia will always prefer a concrete type when possible

```julia
julia> typeof(times2)
typeof(times2)

julia> supertype(typeof(times2))
Function

```

Another way to look at this:

```julia
julia> times2 isa Function
true

julia> isconcretetype(Function)
false

julia> length(subtypes(Function)) # how many things are there that are <: Function
13829

```
