# No typeinference for object call syntax

**URL:** <https://discourse.julialang.org/t/no-typeinference-for-object-call-syntax/19322>\
**Category:** Performance\
**Tags:** question\
**Created:** [January 6, 2019, 11:50am UTC](https://discourse.julialang.org/t/no-typeinference-for-object-call-syntax/19322 "2019-01-06T11:50:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![schlichtanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/schlichtanders/32/32145_2.png) [@schlichtanders](https://discourse.julialang.org/u/schlichtanders)\
**Post date:** [January 6, 2019, 11:50am UTC](https://discourse.julialang.org/t/no-typeinference-for-object-call-syntax/19322/1 "2019-01-06T11:50:00Z")

</div>

I have problems getting type-inference to work when replacing a function with a function object.  
The purpose is to add an extra AbstractType to improve dispatch.

However nothing I tried so far works. Any help is highly welcome!

```julia
function func(T::Type)
  T
end

abstract type AbstractType end
struct Gunc <: AbstractType end
gunc = Gunc()

function (::Gunc)(::Type{T}) where T
  T
end

struct Hunc <: AbstractType end
hunc = Hunc()

function (::Hunc)(::Type{T}) where T
  func(T)
end

struct Junc <: AbstractType end
junc = Junc()

function (::Junc)(T::Type)
  func(T)
end

struct Kunc <: AbstractType end
kunc = Kunc()

@inline function (::Kunc)(::Type{T}) where T
  func(T)
end

func2(args...) = func(args...)
gunc2(args...) = gunc(args...)
hunc2(args...) = hunc(args...)
junc2(args...) = junc(args...)
kunc2(args...) = kunc(args...)

@code_warntype func2(Int) # all fine
@code_warntype gunc2(Int) # ::Any warning
@code_warntype hunc2(Int) # ::Any warning
@code_warntype junc2(Int) # ::Any warning
@code_warntype kunc2(Int) # ::Any warning

```

The code\_warntype output:

```julia
Body::Type{Int64}
36 1 ─ return Int64                                                                         

Body::Any
37 1 ─ %1 = (getfield)(args, 1)::Core.Compiler.Const(Int64, false)                              
   │ %2 = (Main.gunc)(%1)::Any                                                                
   └── return %2                 
                                                          
Body::Any
38 1 ─ %1 = (getfield)(args, 1)::Core.Compiler.Const(Int64, false)                              
   │ %2 = (Main.hunc)(%1)::Any                                                                
   └── return %2                
                                                           
Body::Any
39 1 ─ %1 = (getfield)(args, 1)::Core.Compiler.Const(Int64, false)                              
   │ %2 = (Main.junc)(%1)::Any                                                                
   └── return %2                   
                                                        
Body::Any
40 1 ─ %1 = (getfield)(args, 1)::Core.Compiler.Const(Int64, false)                          
   │ %2 = (Main.kunc)(%1)::Any                                                                
   └── return %2    

```

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [January 6, 2019, 12:38pm UTC](https://discourse.julialang.org/t/no-typeinference-for-object-call-syntax/19322/2 "2019-01-06T12:38:34Z")

</div>

Don’t use non-const global variables (like `gunc` etc). See [Performance Tips · The Julia Language](https://docs.julialang.org/en/v1/manual/performance-tips/index.html#Avoid-global-variables-1).

---

<div class="post-metadata">

**Author:** ![schlichtanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/schlichtanders/32/32145_2.png) [@schlichtanders](https://discourse.julialang.org/u/schlichtanders)\
**Post date:** [January 6, 2019, 1:30pm UTC](https://discourse.julialang.org/t/no-typeinference-for-object-call-syntax/19322/3 "2019-01-06T13:30:37Z")

</div>

thank you so much that was exactly my problem!
