Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Check if parameter of 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 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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading