# Get types of members of a Tuple{} type

**URL:** <https://discourse.julialang.org/t/get-types-of-members-of-a-tuple-type/67645>\
**Category:** Performance\
**Created:** [September 3, 2021, 1:24pm UTC](https://discourse.julialang.org/t/get-types-of-members-of-a-tuple-type/67645 "2021-09-03T13:24:24Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mark\_Nahabedian](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mark_nahabedian/32/16562_2.png) [@Mark\_Nahabedian](https://discourse.julialang.org/u/Mark_Nahabedian)\
**Post date:** [September 3, 2021, 1:24pm UTC](https://discourse.julialang.org/t/get-types-of-members-of-a-tuple-type/67645/1 "2021-09-03T13:24:25Z")

</div>

Given a Tuple type  
`Tuple{typeof(DXFutils.parseraction), Parser, DXFGroup, EntityType_SECTION}`

how do I extract the types of the 2nd and 3rd elements, to get something like

`(DXFGroup, EntityType_SECTION}`)`

Fieldnames

```julia
fieldnames(Tuple{typeof(DXFutils.parseraction), Parser, DXFGroup, EntityType_SECTION})
(1, 2, 3, 4)

```

isn’t helpful. At leat

```julia
getfield(Tuple{typeof(DXFutils.parseraction), Parser, DXFGroup, EntityType_SECTION}, 2)
Any

```

doesn’t get what I’m looking for.

Any suggestions for how to do this?

Thanks.

---

<div class="post-metadata">

**Author:** ![Derek\_Vetsch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/derek_vetsch/32/8552_2.png) [@Derek\_Vetsch](https://discourse.julialang.org/u/Derek_Vetsch)\
**Post date:** [September 3, 2021, 1:42pm UTC](https://discourse.julialang.org/t/get-types-of-members-of-a-tuple-type/67645/2 "2021-09-03T13:42:13Z")

</div>

Two thoughts:

For a tuple t, `[typeof(element) for element in t] ` would give you a `Vector` that you could index into if you only needed the second and third elements i.e.  
`[typeof(element) for element in t][2:3]`  
You could do this same thing by broadcasting if you did `typeof.(t)[2:3]`

And although this may not answer your question exactly, as an aside, things like `eltype.(t)` are really informative in scenarios like this.

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [September 3, 2021, 1:48pm UTC](https://discourse.julialang.org/t/get-types-of-members-of-a-tuple-type/67645/3 "2021-09-03T13:48:49Z")

</div>

```julia
julia> tup = (1,"abc",1+3im)
(1, "abc", 1 + 3im)

julia> typeof.(tup)
(Int64, String, Complex{Int64})

```

edit: already mentioned by @Derek_Vetsch

---

<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:** [September 3, 2021, 1:49pm UTC](https://discourse.julialang.org/t/get-types-of-members-of-a-tuple-type/67645/4 "2021-09-03T13:49:18Z")

</div>

> [@Mark\_Nahabedian](#):
>
> Given a Tuple type

Do you mean a value with that type, or that type variable itself?

I mean, `typeof.(tup)` works if your input is `(1,"abc",1+3im)`. But if

```julia
jl> t = typeof((1,"abc",1+3im))

```

then

```julia
jl> typeof.(t)
DataType

```

---

<div class="post-metadata">

**Author:** ![Mark\_Nahabedian](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mark_nahabedian/32/16562_2.png) [@Mark\_Nahabedian](https://discourse.julialang.org/u/Mark_Nahabedian)\
**Post date:** [September 3, 2021, 1:56pm UTC](https://discourse.julialang.org/t/get-types-of-members-of-a-tuple-type/67645/5 "2021-09-03T13:56:05Z")

</div>

I don’t have a value, I have a type, e.g.

```julia
`Tuple{typeof(DXFutils.parseraction), Parser, DXFGroup, EntityType_SECTION}`

```

as might be the value of `sf.linfo.def.sig`, where sf is a Base.StackTraces.StackFrame.

---

<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:** [September 3, 2021, 2:00pm UTC](https://discourse.julialang.org/t/get-types-of-members-of-a-tuple-type/67645/6 "2021-09-03T14:00:48Z")

</div>

I don’t know the canonical solution, but here are two possibilities:

A hack into the internals of the tuple type:

```julia
jl> t = typeof((1,"abc",1+3im))
Tuple{Int64, String, Complex{Int64}}

jl> t.parameters[2]
String

```

And one more ‘proper’ solution, but a bit inelegant, imho:

```julia
jl> getttypes(::Type{Tuple{T,U,V}}, inds) where {T,U,V} = (T, U, V)[inds]
getttypes (generic function with 4 methods)

jl> getttypes(t, 1)
Int64

jl> getttypes(t, 2:3)
(String, Complex{Int64})

```

---

<div class="post-metadata">

**Author:** ![SteffenPL](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/steffenpl/32/206270_2.png) [@SteffenPL](https://discourse.julialang.org/u/SteffenPL)\
**Post date:** [September 3, 2021, 2:11pm UTC](https://discourse.julialang.org/t/get-types-of-members-of-a-tuple-type/67645/7 "2021-09-03T14:11:56Z")

</div>

Maybe you are looking for the function `fieldtypes`, e.g.

```julia
julia> T = Tuple{Int64,Float64,String}
Tuple{Int64, Float64,String}

julia> fieldtypes(T)[2:3]
(Float64, String)

```

---

<div class="post-metadata">

**Author:** ![Mark\_Nahabedian](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mark_nahabedian/32/16562_2.png) [@Mark\_Nahabedian](https://discourse.julialang.org/u/Mark_Nahabedian)\
**Post date:** [September 3, 2021, 2:35pm UTC](https://discourse.julialang.org/t/get-types-of-members-of-a-tuple-type/67645/8 "2021-09-03T14:35:50Z")

</div>

Thanks to all those who responded. `fieldtypes` seems to be exactly what I’m looking for.
