Get types of members of a Tuple{} type

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(Tuple{typeof(DXFutils.parseraction), Parser, DXFGroup, EntityType_SECTION})
(1, 2, 3, 4)

isn’t helpful. At leat

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

doesn’t get what I’m looking for.

Any suggestions for how to do this?


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.

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

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

edit: already mentioned by @Derek_Vetsch

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

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


jl> typeof.(t)

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

`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.

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

A hack into the internals of the tuple type:

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

jl> t.parameters[2]

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

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)

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

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

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

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

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