Search code examples
functionparametersjuliasingletontypeof

Check if a parameter of a function is a function


I have a function:

function f(a)
   do something that depends on a
end

which behaviour depends on the parameter a. This can be a string, an int or a function itself. For this, I want to check if the parameter is a function itself.

function f(a)
   if typeof(a) == int
      ...
   end
   ...
end

I tried to use typeof(a). If a is a function, I get :

typeof(a) (singleton type of function a, subtype of Function)

but if I then use:

typeof(a) == Function

it is false.


Solution

  • You can use isa() for this. A simple example:

    julia> f(x) = x
    f (generic function with 1 method)
    
    julia> isa(f, Function)
    true
    

    isa can also be used as an infix operator:

    julia> f isa Function
    true