Reputation: 511
I am defining a function that will take a new function from a user and do some "maths". I am clearly blank about this user defined function because I don't know what his/he inputs to his function are. I would need to know these user inputs later in the "maths" that my function is doing.
This is my function, for example,
function userfunction(x,y)
return x+y
end
Just for example, I am clearly unaware of the inputs x
and y
in the above user-defined function. I use this function to do some maths in my function but I need to be aware that x
and y
are the inputs of the userfunction
.
function myfun(userfunction::Function)
....do so maths...
#but some maths here need to know the inputs of "userfunction"
end
Is there a function in Julia that can read the inputs of another function? Like for instance, read the inputs of "userfunction" and return an array?
Upvotes: 1
Views: 176
Reputation: 1991
I'm not sure exactly what you're looking for, but if you want to know what kinds of arguments your userfunction
takes, you can get that.
First, remember that userfunction
can have multiple method, each with different arguments (yay, multiple dispatch!). To get an array of them all, do:
meths = collect(methods(div))
Then, for each method, you can look up its signature:
signature = meths[1].sig
, which will give you a type Tuple{typeof(div), Int, Int}
, for example.
Then, discard the first element of the signature to get the arguments:
arguments = Tuple(signature.parameters[2:end])
, to get the argument types: (Int, Int)
.
That's pretty complicated, and perhaps there's an easier way.
Upvotes: 2