# Obtaining parameter types for a function

**URL:** https://discourse.julialang.org/t/obtaining-parameter-types-for-a-function/62169
**Category:** General Usage
**Tags:** type
**Created:** [June 1, 2021, 12:53am UTC](https://discourse.julialang.org/t/obtaining-parameter-types-for-a-function/62169 "2021-06-01T00:53:41Z")
**Posts on this page:** 14
**Page:** 1

<div class="post-metadata">

### Author: ![harris-chris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/harris-chris/32/23877_2.png) [@harris-chris](https://discourse.julialang.org/u/harris-chris)
#### Post date: [June 1, 2021, 12:53am UTC](https://discourse.julialang.org/t/obtaining-parameter-types-for-a-function/62169/1 "2021-06-01T00:53:41Z")

</div>

Hello all,

Is there any way to obtain the parameter types for a given method? For example, if I have `function f(x::Int64) x end`, is there a way to recover the type of parameter x? I realize that generic methods may make this complex or impossible but I thought I’d ask.

Thanks!

---

<div class="post-metadata">

### Author: ![ettersi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ettersi/32/6829_2.png) [@ettersi](https://discourse.julialang.org/u/ettersi)
#### Post date: [June 1, 2021, 1:06am UTC](https://discourse.julialang.org/t/obtaining-parameter-types-for-a-function/62169/2 "2021-06-01T01:06:06Z")

</div>

Just getting the obvious out of the way:

```julia
julia> function f(x::Int)
           @show typeof(x)
           return x
       end;

julia> f(1);
typeof(x) = Int64

```

I assume this is not what you want?

---

<div class="post-metadata">

### Author: ![harris-chris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/harris-chris/32/23877_2.png) [@harris-chris](https://discourse.julialang.org/u/harris-chris)
#### Post date: [June 1, 2021, 1:18am UTC](https://discourse.julialang.org/t/obtaining-parameter-types-for-a-function/62169/3 "2021-06-01T01:18:39Z")

</div>

Yes, sorry, I should have been clearer. What I imagine is more like:

```julia
julia> parameter_types(f);
x::Int64

```

---

<div class="post-metadata">

### Author: ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)
#### Post date: [June 1, 2021, 1:27am UTC](https://discourse.julialang.org/t/obtaining-parameter-types-for-a-function/62169/4 "2021-06-01T01:27:12Z")

</div>

That’s not a well posed idea in Julia because functions can have as many heterogeneous methods as you like.

```julia
julia> f(x::Int) = x + 1
f (generic function with 1 method)

julia> f(s::String) = length(s)
f (generic function with 2 methods)

julia> f(v::Vector) = v'v
f (generic function with 3 methods)

julia> f(1), f("hi"), f([1,2,3])
(2, 2, 14)

```

Only methods have ‘parameters’, though we prefer the term signature here.

```julia
julia> methods(f)
# 3 methods for generic function "f":
[1] f(x::Int64) in Main at REPL[5]:1
[2] f(s::String) in Main at REPL[6]:1
[3] f(v::Vector{T} where T) in Main at REPL[7]:1

```

Methods themselves aren’t really first-class objects in julia though, so there’s no blessed api to ask a specific method what it’s signature is.

Depending on your usecase however, one thing you could use is `hasmethod`, e.g.

```julia
julia> hasmethod(f, (Int,))
true

julia> hasmethod(f, (Int, Int))
false

julia> hasmethod(f, (String, ))
true

```

---

<div class="post-metadata">

### Author: ![ettersi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ettersi/32/6829_2.png) [@ettersi](https://discourse.julialang.org/u/ettersi)
#### Post date: [June 1, 2021, 1:32am UTC](https://discourse.julialang.org/t/obtaining-parameter-types-for-a-function/62169/5 "2021-06-01T01:32:56Z")

</div>

Probably not official syntax, but you can do the following:

```julia
julia> f(x::Int) = x
       f(x::Float64) = x;

julia> methods(f).ms[1].sig
Tuple{typeof(f), Int64}

julia> methods(f).ms[2].sig
Tuple{typeof(f), Float64}

```

---

<div class="post-metadata">

### Author: ![harris-chris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/harris-chris/32/23877_2.png) [@harris-chris](https://discourse.julialang.org/u/harris-chris)
#### Post date: [June 1, 2021, 1:50am UTC](https://discourse.julialang.org/t/obtaining-parameter-types-for-a-function/62169/6 "2021-06-01T01:50:49Z")

</div>

Thanks - so `methods(f).ms[2].sig` (for example) is actually returning a type. Is there any way to then index this type so as to get the second element?

---

<div class="post-metadata">

### Author: ![harris-chris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/harris-chris/32/23877_2.png) [@harris-chris](https://discourse.julialang.org/u/harris-chris)
#### Post date: [June 1, 2021, 1:51am UTC](https://discourse.julialang.org/t/obtaining-parameter-types-for-a-function/62169/7 "2021-06-01T01:51:49Z")

</div>

Thank you - iterating through a list of potential input types like this may be the best way to go.

---

<div class="post-metadata">

### Author: ![ettersi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ettersi/32/6829_2.png) [@ettersi](https://discourse.julialang.org/u/ettersi)
#### Post date: [June 1, 2021, 2:04am UTC](https://discourse.julialang.org/t/obtaining-parameter-types-for-a-function/62169/8 "2021-06-01T02:04:23Z")

</div>

That’s the best I could come up with:

```julia
julia> second_type(::Type{Tuple{F,T}}) where {F,T} = T
second_type (generic function with 3 methods)

julia> second_type(methods(f).ms[1].sig)
Int64

```

It’s not getting any prettier… ☹

---

<div class="post-metadata">

### Author: ![CameronBieganek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cameronbieganek/32/6915_2.png) [@CameronBieganek](https://discourse.julialang.org/u/CameronBieganek)
#### Post date: [June 1, 2021, 3:06am UTC](https://discourse.julialang.org/t/obtaining-parameter-types-for-a-function/62169/9 "2021-06-01T03:06:19Z")

</div>

For the sub-problem of getting a particular element in `Tuple{...}`:

```julia
julia> fieldtypes(Tuple{Int, String})
(Int64, String)

julia> fieldtypes(Tuple{Int, String})[2]
String

```

---

<div class="post-metadata">

### Author: ![harris-chris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/harris-chris/32/23877_2.png) [@harris-chris](https://discourse.julialang.org/u/harris-chris)
#### Post date: [June 1, 2021, 3:37am UTC](https://discourse.julialang.org/t/obtaining-parameter-types-for-a-function/62169/10 "2021-06-01T03:37:11Z")

</div>

Haha. But all useful things have their beauty.

---

<div class="post-metadata">

### Author: ![harris-chris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/harris-chris/32/23877_2.png) [@harris-chris](https://discourse.julialang.org/u/harris-chris)
#### Post date: [June 1, 2021, 3:37am UTC](https://discourse.julialang.org/t/obtaining-parameter-types-for-a-function/62169/11 "2021-06-01T03:37:34Z")

</div>

Right, thanks. That’s perfect then. Thanks all, it’s been educational.

---

<div class="post-metadata">

### Author: ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)
#### Post date: [June 1, 2021, 4:44am UTC](https://discourse.julialang.org/t/obtaining-parameter-types-for-a-function/62169/12 "2021-06-01T04:44:26Z")

</div>

> [@harris-chris](#):
>
> But all useful things have their beauty.

I’m curious. What’s this useful for?

---

<div class="post-metadata">

### Author: ![harris-chris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/harris-chris/32/23877_2.png) [@harris-chris](https://discourse.julialang.org/u/harris-chris)
#### Post date: [June 1, 2021, 5:06am UTC](https://discourse.julialang.org/t/obtaining-parameter-types-for-a-function/62169/13 "2021-06-01T05:06:06Z")

</div>

My original question? It’s a bit niche but I have a situation where the package user can pass a function. To make this as ergonomic as possible for the user, I’d like to write it such that the function will take either one of the JSON types (array, string, object/dict etc), or a struct that the JSON is expected to parse to, as its only argument. The JSON can then be parsed in advance into the appropriate type. But obviously this requires me to be able to find the function’s parameter type.

---

<div class="post-metadata">

### Author: ![Brian1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brian1/32/49250_2.png) [@Brian1](https://discourse.julialang.org/u/Brian1)
#### Post date: [March 11, 2022, 7:31am UTC](https://discourse.julialang.org/t/obtaining-parameter-types-for-a-function/62169/14 "2022-03-11T07:31:22Z")

</div>

Does this work?

```julia
methods(f)[1].sig.parameters[2]

```
