Functions and code_warntype

I am puzzled how functions are handled by type inference. There doesn’t seem to be a type Function{InputType, ReturnType} to help type inference, instead there is a typeof(f) Type. Why is that? I am happy if you point me where to read up on this.

julia> h(f::Function, x) = f(x)
h (generic function with 1 method)

julia> code_warntype(h, (Function, Int))
Body::Any
1 1 ─ %1 = (f)(x)::Any                                                                                                                                                                                                                     │
  └──      return %1                                                                                                                                                                                                                       │

julia> @code_warntype h(abs, 3)
Body::Int64
1 1 ─ %1 = (Base.flipsign_int)(x, x)::Int64                                                                                                                                                                                          │╻╷ abs
  └──      return %1                                                                                                                                                                                                                 │  

julia> @macroexpand @code_warntype h(abs, 3)
:((InteractiveUtils.code_warntype)(h, (Base.typesof)(abs, 3)))

julia> typeof(abs)
typeof(abs)

julia> code_warntype(h, (typeof(abs), Int))
Body::Int64
1 1 ─ %1 = (Base.flipsign_int)(x, x)::Int64                                                                                                                                                                                          │╻╷ abs
  └──      return %1   

You may find discussions about arrow types interesting (search the discussions, also older ones in Google Groups).

Check Jeff’s thesis: https://dspace.mit.edu/bitstream/handle/1721.1/99811/927297147-MIT.pdf?sequence=1

1 Like