# Iterating over types of tuple elements in @generated function

**URL:** <https://discourse.julialang.org/t/iterating-over-types-of-tuple-elements-in-generated-function/46271>\
**Category:** New to Julia\
**Created:** [September 8, 2020, 3:15pm UTC](https://discourse.julialang.org/t/iterating-over-types-of-tuple-elements-in-generated-function/46271 "2020-09-08T15:15:35Z")\
**Posts on this page:** 3\
**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:** [September 8, 2020, 3:15pm UTC](https://discourse.julialang.org/t/iterating-over-types-of-tuple-elements-in-generated-function/46271/1 "2020-09-08T15:15:35Z")

</div>

I am writing a generated function and want to iterate over the types of the inputs to generate some splendid code. This works

```julia
@generated function foo(t...)
    for c ∈ t 
        Core.println(c)
    end
    return :( Core.println("foo !") )
end
foo(3.,1)

Float64
Int64
foo !

```

But for reasons I will not go into, I would like the inputs to be provided packed in a tuple. The following code does not work:

```julia
@generated function bar(t::Tuple)
    for c ∈ t # !!! The type "Tuple{This,That}" is not iterable
        Core.println(c)
    end
    return :( Core.println("bar !") )
end
bar((3.,1))

MethodError: no method matching iterate(::Type{Tuple{Float64,Int64}})

```

The error message says it all: in the body of bar, t is the type Tuple{Float64,Int64} and one cannot iterate that. Makes sense. But can I obtain an iterator that generates Float64 and Int64?

I have a workaround, that works because I will only have one tuple in my real problem.

```julia
@generated function bar(t::Tuple)
    return :( foo(t...) )
end

```

Any better ideas?

---

<div class="post-metadata">

**Author:** ![lucas711642](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lucas711642/32/12051_2.png) [@lucas711642](https://discourse.julialang.org/u/lucas711642)\
**Post date:** [September 8, 2020, 3:32pm UTC](https://discourse.julialang.org/t/iterating-over-types-of-tuple-elements-in-generated-function/46271/2 "2020-09-08T15:32:24Z")

</div>

Use the `fieldtypes` function:

```julia
fieldtypes(Tuple{Float64, Int}) === (Float64, Int) # true

@generated function bar(t::Tuple)
    for c ∈ fieldtypes(t)
        Core.println(c)
    end
    return :( Core.println("bar !") )
end
bar((3.,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:** [September 8, 2020, 3:47pm UTC](https://discourse.julialang.org/t/iterating-over-types-of-tuple-elements-in-generated-function/46271/3 "2020-09-08T15:47:45Z")

</div>

Everything works!!! 😀 and I get wiser.

Thanks Lucas!
