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 it self. 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.
CodePudding user response:
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
CodePudding user response:
You might also consider using Julia's Multiple Dispatch feature:
function f(x::Function)
# do something with a Function
end
funcion f(x::Int)
# do something with an Int
end
instead of using conditions inside the function.
This has the benefit of being faster if the compiler can do the type inference during compile-time and being more Julian.