Extract arguments type and defalut value from a Function

question

#1

hello, everyone,
I want to extract arguments type and defalut value from a Function, but method() only give part of arguement type and no defalut value which are defined by function.

function hello(name::String="world";greet::String="nice day")
    println("hello $name, $greet")
end
methods(hello)

will output this:

2 methods for generic function hello:
    hello(name::String; greet) in Main at In[6]:2
    hello() in Main at In[6]:2

Is there other ways to do this?

I am using julia1.0.2,thanks ~

Regards


#2
dump([m for m in methods(hello)])

shows information for both methods.

julia> [m for m in methods(hello)][1].roots[2]
"nice day"

But I have no idea if there is an official public API to get at this kind of info.


#3

I tried to address a similar problem with a macro recently.
See here.


#4

thanks very much~ then I tried this, it can get the name and greet default name, but no number argument.

function hello(name::String="world";greet::String="nice day",number::Int=3)
    println("x")
end
methods(hello)
for m in methods(hello)
    println(m.roots)
end

and output this

Any[Symbol("#hello#36"), "nice day", :hello, Symbol("In[35]")]
Any["world", :hello, Symbol("In[35]")]

#5

@jandehaan @jbrea yes, I hoped there is a public API for this.