Stream: helpdesk (published)

Topic: Can we get the types of `args` from a function?


view this post on Zulip Davi Sales Barreira (Aug 18 2022 at 01:17):

Suppose I have a function such as:

function f(x::Int,y::Float64)::Real
      return y^x
end

Is there a way to get the domain type from this function (and possibly the "codomain", which in this case was ::Real)? I'm supposing that this function has only one method. And in case there are many, one can just take the first method asfirst(methods(f)).

I wish to get something like Tuple{Int,Float64} for the domain type, and Real for the codomain.

P.S: I already have a macro that can do this during the function declaration, but I was wondering if one can do it after the function is already declared.

view this post on Zulip Zachary P Christensen (Aug 18 2022 at 03:48):

Are you talking about looking things up in the method table?

view this post on Zulip Jakob Nybo Nissen (Aug 18 2022 at 04:56):

methods(f).sig not sure about annotated return type

view this post on Zulip Sukera (Aug 18 2022 at 05:28):

annotated return types just insert a convert at return points, it doesn't show up in the method signature (that's inference' domain)

view this post on Zulip Sukera (Aug 18 2022 at 05:28):

in general, you'll need to know the input types to your method to get the output type(s) (there may be multiple for type unstable functions)

view this post on Zulip Jakob Nybo Nissen (Aug 18 2022 at 05:30):

It also inserts a typeassert, right? But yeah okay

view this post on Zulip Sukera (Aug 18 2022 at 05:30):

yes

view this post on Zulip Mason Protter (Aug 18 2022 at 05:33):

After the convert, yeah

view this post on Zulip Davi Sales Barreira (Aug 18 2022 at 12:50):

Thanks for the replies! Here is the solution I came up with. Feel free to tell me how to improve it:

function getfunctiontype(f, methodindex=1)
    m = collect(methods(f))[methodindex]
    dom = Tuple([m.sig.types[2:end]...])
    codom = Base.code_typed(f, dom)[1].second

    if length(dom) == 1
        dom = dom[1]
    end
    return dom,codom
end

This if length(dom) == 1 is ugly, but I don't know how to avoid it.

view this post on Zulip Sundar R (Aug 18 2022 at 13:38):

Do you need it? Isn't it better to return a Tuple in all cases, do you need a special check for the one argument case?

view this post on Zulip Leandro Martínez (Aug 18 2022 at 14:41):

Just for curiosity, why are you willing to get these signatures? That feels like a XY problem, unless you are doing something very low level.

view this post on Zulip Davi Sales Barreira (Aug 18 2022 at 22:39):

Sundar R said:

Do you need it? Isn't it better to return a Tuple in all cases, do you need a special check for the one argument case?

I wrote a composition that is restricted on types, so Tuple{Int} == Int fails. Hence, I either fix this here or in the composition implementation.
I thought fixing here would be better cause it then returns the actual types.


Last updated: Oct 02 2023 at 04:34 UTC