What is typeof(fun)?

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.

In this context, map is restricted to methods of zero. In Julia, every function has its own type . When you specialize a method for a given function, you need to restrict allowed input argument so the typeof(fun). Function is supertype of all functions and it is abstract.

4 Likes

Note that if typeof(f) were Function, functions like map and broadcast would have to be type unstable, because they couldn’t specialize on the particular functions you’re passing to them.

For writing specific methods that handle a particular function in a special way (eg, zero), knowing that the type of zero is typeof(zero) is a lot easier than if it were some mangled name.

2 Likes

thanks.

as I remembered, official docs does not mention it?

I gave a fairly detailed stack overflow response on exactly this question here

Cheers,

Colin

1 Like

I’ve just submitted the issue in GitHub.