# Get type of field in parametric type

**URL:** https://discourse.julialang.org/t/get-type-of-field-in-parametric-type/8210
**Category:** General Usage
**Tags:** parametric-types
**Created:** [January 6, 2018, 11:41pm UTC](https://discourse.julialang.org/t/get-type-of-field-in-parametric-type/8210 "2018-01-06T23:41:09Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![tpoisot](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tpoisot/32/19812_2.png) [@tpoisot](https://discourse.julialang.org/u/tpoisot)
#### Post date: [January 6, 2018, 11:41pm UTC](https://discourse.julialang.org/t/get-type-of-field-in-parametric-type/8210/1 "2018-01-06T23:41:09Z")

</div>

Let’s say I have a parametric type defined as

```julia
mutable struct Example{T,N<:Real}
  a::T
  b::Array{N,1}
end

```

I can create an object with something like,

```julia
x = Example(:a, [1, 2, 3])

```

Then the type of `x` is `Example{Symbol, Int64}` – is there a way to get `Int64`? So far I found

```julia
eltype(fieldtype(typeof(x), 2))

```

which works, but I was wondering whether there is a more julianesque way of getting the same result?

---

<div class="post-metadata">

### Author: ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)
#### Post date: [January 6, 2018, 11:47pm UTC](https://discourse.julialang.org/t/get-type-of-field-in-parametric-type/8210/2 "2018-01-06T23:47:15Z")

</div>

```julia
Base.@pure get_type_parameter(x) = typeof(x).parameters[2]

```

That infers.

```julia
julia> Base.@pure get_type_parameter(x) = typeof(x).parameters[2]
get_type_parameter (generic function with 2 methods)

julia> @code_warntype get_type_parameter(rand(3))
Variables:
  #self# <optimized out>
  x <optimized out>

Body:
  begin
      return $(Expr(:invoke, MethodInstance for getindex(::SimpleVector, ::Int64), :(Main.getindex), :((Core.getfield)(Array{Float64,1}, :parameters)::SimpleVector), 2))
  end::Int64

```

The more general form

```julia
Base.@pure get_type_parameter(x,i) = typeof(x).parameters[i]

```

needs IPO from v0.7 to infer.

---

<div class="post-metadata">

### Author: ![piever](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/piever/32/1815_2.png) [@piever](https://discourse.julialang.org/u/piever)
#### Post date: [January 6, 2018, 11:54pm UTC](https://discourse.julialang.org/t/get-type-of-field-in-parametric-type/8210/3 "2018-01-06T23:54:12Z")

</div>

You probably thought about this already, but the Julian way would be to dispatch on `Example` every time you need to use the type parameter `T`. For example:

```julia
julia> findparam(ex::Example{T, N}) where {T, N} = N
findparam (generic function with 1 method)

julia> x = Example(:a, [1, 2, 3])
Example{Symbol,Int64}(:a, [1, 2, 3])

julia> findparam(x)
Int64

```

---

<div class="post-metadata">

### Author: ![tpoisot](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tpoisot/32/19812_2.png) [@tpoisot](https://discourse.julialang.org/u/tpoisot)
#### Post date: [January 7, 2018, 2:37am UTC](https://discourse.julialang.org/t/get-type-of-field-in-parametric-type/8210/4 "2018-01-07T02:37:47Z")

</div>

This is precisely what I was failing to express! Thanks!

---

<div class="post-metadata">

### Author: ![jameson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jameson/32/23_2.png) [@jameson](https://discourse.julialang.org/u/jameson)
#### Post date: [January 12, 2018, 12:44am UTC](https://discourse.julialang.org/t/get-type-of-field-in-parametric-type/8210/5 "2018-01-12T00:44:20Z")

</div>

> [@ChrisRackauckas](#):
>
> Base.@pure  
> That infers.

Please don’t suggest the use of this annotation. It’s nearly always wrong, and thus actively unhelpful to mention it, then have to explain that it also causes inference to be fatally wrong. It doesn’t infer, it does the opposite: it tells inference that it’s OK to totally disregard correctness. (piever’s solution is correct).
