# Capturing the types of the values, from the type of a Named Tuple

**URL:** <https://discourse.julialang.org/t/capturing-the-types-of-the-values-from-the-type-of-a-named-tuple/116199>\
**Category:** General Usage\
**Tags:** parametric-types\
**Created:** [June 25, 2024, 12:31pm UTC](https://discourse.julialang.org/t/capturing-the-types-of-the-values-from-the-type-of-a-named-tuple/116199 "2024-06-25T12:31:55Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Philippe\_Maincon1](https://avatars.discourse-cdn.com/v4/letter/p/ec9cab/32.png) [@Philippe\_Maincon1](https://discourse.julialang.org/u/Philippe_Maincon1)\
**Post date:** [June 25, 2024, 12:31pm UTC](https://discourse.julialang.org/t/capturing-the-types-of-the-values-from-the-type-of-a-named-tuple/116199/1 "2024-06-25T12:31:55Z")

</div>

Hi,

Given the type of a `NamedTuple`, I want to make operations on the types of the values (or fields) of the type, ultimately to compute a new type and allocate memory.

Here is a little code

```julia
function see(::Type{NT}) where{K,V,NT<:NamedTuple{K,V}} 
    @show K
    @show V
    get(V)
    return
end
function see(::Type{V}) where{A,B,V<:Tuple{A,B}}     
    @show A,B
    return
end

T = typeof((a=(1,2.),b=:hi))
see(T)

```

which displays

```julia
K = (:a, :b)
V = Tuple{Tuple{Int64, Float64}, Symbol}
(A, B) = (Tuple{Int64, Float64}, Symbol)

```

as wanted: `A`, and `B` contain the types of the values of a variable of type `T`.

So far so good. The catch is, I want to be able to analyse the type `T` of `NamedTuple`s of arbitrary length. Nothing loath, I try

```julia
function see(::Type{V}) where{A...,V<:Tuple{A...}}     
    @show A...
    return
end

```

but get the error

```julia
ERROR: syntax: invalid variable expression in "where" 
around [the new method]

```

So slurping isn’t the thing in `where` constructs. New attempt

```julia
function see(::Type{V}) where{A,V<:Tuple{A}}     
    @show A
    return
end

```

where I again hope `A` will be a `Tuple` of `Type`s.

```julia
ERROR: MethodError: no method matching 
see(::Type{Tuple{Tuple{Int64, Float64}, Symbol}})
Closest candidates are: [...]

```

Any clever ideas?  
🙂

---

<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:** [June 25, 2024, 12:38pm UTC](https://discourse.julialang.org/t/capturing-the-types-of-the-values-from-the-type-of-a-named-tuple/116199/2 "2024-06-25T12:38:00Z")

</div>

```julia-repl
julia> typeof.(values((a = 1.0, b = 1, c = "A")))
(Float64, Int64, String)

```

With this you get the types from the values. For tuples that should be quite general, since the tuple type is by definition that of the values. Not sure if that helps.

---

<div class="post-metadata">

**Author:** ![Philippe\_Maincon1](https://avatars.discourse-cdn.com/v4/letter/p/ec9cab/32.png) [@Philippe\_Maincon1](https://discourse.julialang.org/u/Philippe_Maincon1)\
**Post date:** [June 25, 2024, 1:06pm UTC](https://discourse.julialang.org/t/capturing-the-types-of-the-values-from-the-type-of-a-named-tuple/116199/3 "2024-06-25T13:06:00Z")

</div>

Hi Imiq,

Thank you. That would work (actually, I did implement something along those lines), but I was trying to create functions that operate purely on the types.

In other words: if I give you

```julia
typeof((a = 1.0, b = 1, c = "A"))

```

as input, do you know any way to give me back

```julia
(Float64, Int64, String)

```

that is not specific to the length of the input `NamedTuple`?

---

<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:** [June 25, 2024, 1:36pm UTC](https://discourse.julialang.org/t/capturing-the-types-of-the-values-from-the-type-of-a-named-tuple/116199/4 "2024-06-25T13:36:26Z")

</div>

I’m not sure if I understand the question:

```julia-repl
julia> t(x) = (typeof.(values(x)))
t (generic function with 1 method)

julia> t((a=1.0, b=1, c="A"))
(Float64, Int64, String)

julia> t((a=1.0, b=1))
(Float64, Int64)

```

How to operate on the type directly I don’t know. But that, _for tuples_ is probably not particularly useful (at least I do not see how it could be).

---

<div class="post-metadata">

**Author:** ![mikmoore](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mikmoore/32/31109_2.png) [@mikmoore](https://discourse.julialang.org/u/mikmoore)\
**Post date:** [June 25, 2024, 1:38pm UTC](https://discourse.julialang.org/t/capturing-the-types-of-the-values-from-the-type-of-a-named-tuple/116199/5 "2024-06-25T13:38:27Z")

</div>

> **EDIT: hidden because this is terrible and should not be done**
>
> _This is using internals and is highly discouraged: It may break in any future release and it might be broken for certain uses currently._
> 
> ```julia-repl
> julia> t = Tuple{Int,Float64}
> Tuple{Int64, Float64}
> 
> julia> t.types
> svec(Int64, Float64)
> 
> ```
> 
> I think that [#53193](https://github.com/JuliaLang/julia/issues/53193) tangentially relates to this, although that is dealing with `Union` rather than `Tuple`.

* * *

I would encourage you to give a little more info about your problem. It may be that there is an idiomatic way to achieve what you want that doesn’t require this type of thing.

---

<div class="post-metadata">

**Author:** ![jipolanco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jipolanco/32/12129_2.png) [@jipolanco](https://discourse.julialang.org/u/jipolanco)\
**Post date:** [June 25, 2024, 1:47pm UTC](https://discourse.julialang.org/t/capturing-the-types-of-the-values-from-the-type-of-a-named-tuple/116199/6 "2024-06-25T13:47:58Z")

</div>

Not sure this answers the question, but maybe `fieldtypes` does what you’re looking for:

```julia-repl
julia> nt = (a = 1.0, b = 1, c = "A")
(a = 1.0, b = 1, c = "A")

julia> T = typeof(nt)
@NamedTuple{a::Float64, b::Int64, c::String}

julia> fieldtypes(T)
(Float64, Int64, String)

```

---

<div class="post-metadata">

**Author:** ![Philippe\_Maincon1](https://avatars.discourse-cdn.com/v4/letter/p/ec9cab/32.png) [@Philippe\_Maincon1](https://discourse.julialang.org/u/Philippe_Maincon1)\
**Post date:** [June 25, 2024, 1:49pm UTC](https://discourse.julialang.org/t/capturing-the-types-of-the-values-from-the-type-of-a-named-tuple/116199/7 "2024-06-25T13:49:35Z")

</div>

Jiplanco:

BINGO!!! Thank you!!!

Imiq, since you ask about the context: I want to preallocate memory for information (in nested structures of Tuples and NamedTuples, where the Float64’s can be replaced by ForwardDiff-type variables.

Case closed. Thank you, friends!
