# How can i index a tuple type?

**URL:** https://discourse.julialang.org/t/how-can-i-index-a-tuple-type/76501
**Category:** New to Julia
**Created:** [February 15, 2022, 5:55pm UTC](https://discourse.julialang.org/t/how-can-i-index-a-tuple-type/76501 "2022-02-15T17:55:54Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![lrnv](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lrnv/32/19373_2.png) [@lrnv](https://discourse.julialang.org/u/lrnv)
#### Post date: [February 15, 2022, 5:55pm UTC](https://discourse.julialang.org/t/how-can-i-index-a-tuple-type/76501/1 "2022-02-15T17:55:54Z")

</div>

Hey,

Say i have a tuple type `Tuple{A,B}`. How can i index it to ge the `n`th type ? I have the following error:

```julia
julia> Tuple{Float64,Int64}[1]
ERROR: MethodError: Cannot `convert` an object of type Int64 to an object of type Tuple{Float64, Int64}
Closest candidates are:
  convert(::Type{T}, ::T) where T<:Tuple at essentials.jl:315
  convert(::Type{T}, ::Tuple{Vararg{Any, N}}) where {N, T<:Tuple} at essentials.jl:316
  convert(::Type{T}, ::CartesianIndex) where T<:Tuple at multidimensional.jl:137
  ...
Stacktrace:
 [1] setindex!(A::Vector{Tuple{Float64, Int64}}, x::Int64, i1::Int64)
   @ Base .\array.jl:839
 [2] getindex(#unused#::Type{Tuple{Float64, Int64}}, x::Int64)
   @ Base .\array.jl:392
 [3] top-level scope
   @ REPL[72]:1

julia> 

```

I dont want to instantiate the Tuple type to index a tuple and then get the type.

---

<div class="post-metadata">

### Author: ![Christopher\_Fisher](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christopher_fisher/32/26132_2.png) [@Christopher\_Fisher](https://discourse.julialang.org/u/Christopher_Fisher)
#### Post date: [February 15, 2022, 6:00pm UTC](https://discourse.julialang.org/t/how-can-i-index-a-tuple-type/76501/2 "2022-02-15T18:00:27Z")

</div>

If `T` is the type of your tuple, then `T.types[1]` will provide the type of the first element.

---

<div class="post-metadata">

### Author: ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)
#### Post date: [February 15, 2022, 8:08pm UTC](https://discourse.julialang.org/t/how-can-i-index-a-tuple-type/76501/3 "2022-02-15T20:08:16Z")

</div>

Is this an implementation detail or something that can be relied upon changes of minor version?

---

<div class="post-metadata">

### Author: ![lrnv](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lrnv/32/19373_2.png) [@lrnv](https://discourse.julialang.org/u/lrnv)
#### Post date: [February 15, 2022, 8:35pm UTC](https://discourse.julialang.org/t/how-can-i-index-a-tuple-type/76501/4 "2022-02-15T20:35:28Z")

</div>

Looks like `T.parameters` also gets there. Dunno if it can be relied upon however

---

<div class="post-metadata">

### Author: ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)
#### Post date: [February 15, 2022, 8:45pm UTC](https://discourse.julialang.org/t/how-can-i-index-a-tuple-type/76501/5 "2022-02-15T20:45:40Z")

</div>

not very reliable, more importantly, the order can definitely change

---

<div class="post-metadata">

### Author: ![raminammour](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raminammour/32/13572_2.png) [@raminammour](https://discourse.julialang.org/u/raminammour)
#### Post date: [February 15, 2022, 10:28pm UTC](https://discourse.julialang.org/t/how-can-i-index-a-tuple-type/76501/6 "2022-02-15T22:28:08Z")

</div>

Inspired by [this line](https://github.com/JuliaLang/julia/blob/2db86f2360a03530b34b1d35bd86fb53fa841422/base/deprecated.jl#L228) from `Base`:

```julia
tuple_type_head(T::Type) = fieldtype(T, 1)

```

It looks like `fieldtypes(T,n)` should do the job, which is exported. However, the comment just above that line, and the fact that it is in `deprecated.jl` calls for caution…
