# Tuple with conditional entries

**URL:** <https://discourse.julialang.org/t/tuple-with-conditional-entries/93052>\
**Category:** General Usage\
**Created:** [January 16, 2023, 10:48pm UTC](https://discourse.julialang.org/t/tuple-with-conditional-entries/93052 "2023-01-16T22:48:12Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jlbosse](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jlbosse/32/11274_2.png) [@jlbosse](https://discourse.julialang.org/u/jlbosse)\
**Post date:** [January 16, 2023, 10:48pm UTC](https://discourse.julialang.org/t/tuple-with-conditional-entries/93052/1 "2023-01-16T22:48:13Z")

</div>

Is it possible create a tuple with the existence of entries conditioned on some variables?

I.e. what I want is something like

```julia
mytuple = (42 if condition1, pi condition2 )
# == (42, pi) if condition1 and condition2 are true
# == (42,) if condition1 is true and condition2 is false
# ...

```

I know that I could do something like

```julia
mytuple = if condition1 && condition2
    (42, pi)
elseif condition1 && !condition2
   (42,)
elseif
   ...
end

```

but I have quite a few conditions and possible entries in the tuple s.t. enumerating all possible combinations would get quite lengthy.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [January 16, 2023, 10:54pm UTC](https://discourse.julialang.org/t/tuple-with-conditional-entries/93052/2 "2023-01-16T22:54:05Z")

</div>

> [@jlbosse](#):
>
> Is it possible create a tuple with the existence of entries conditioned on some variables?

Probably a tuple is the wrong data structure in this case. Tuples are mainly good for situations in which the size (and types) of the entries are known statically (at compile time, not runtime).

What underlying problem are you trying to solve?

---

<div class="post-metadata">

**Author:** ![jlbosse](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jlbosse/32/11274_2.png) [@jlbosse](https://discourse.julialang.org/u/jlbosse)\
**Post date:** [January 16, 2023, 11:27pm UTC](https://discourse.julialang.org/t/tuple-with-conditional-entries/93052/3 "2023-01-16T23:27:35Z")

</div>

The use case is inside a `@generated` function where I want to generate different code depending on the parameters of a parametric type. See the following MWE for what I am trying to achieve:

```julia
struct MyRecord{R1,R2,R3}
    data::DataFrame
end

@generated function MyRecord{R1,R2,R3}()
    return quote
        rows = $(:(:var1 => Int[] if R1, :var2 => Float64[] if R2, :var3 => Int[] if R3))
        data = DataFrame(rows...)
        return MyRecord{R1,R2,R3}(data)
    end
end

@generated function Base.push!(records::MyRecord{R1,R2,R3}, var1, var2, var3) where {R1,R2,R3}
    return quote
        row = $(:(var1 if R1, var2 if R2, var3 if R3))
        push!(records.data, row)
    end
end

```

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [January 17, 2023, 2:02am UTC](https://discourse.julialang.org/t/tuple-with-conditional-entries/93052/4 "2023-01-17T02:02:39Z")

</div>

> [@jlbosse](#):
>
> The use case is inside a `@generated` function where I want to generate different code depending on the parameters of a parametric type.

In that case, performance doesn’t matter since it is happening at compile time, so I would just do it as an array and then convert back to a tuple, e.g.:

```julia
@generated function Base.push!(records::MyRecord{R1,R2,R3}, var1, var2, var3) where {R1,R2,R3}
    rowexpr = Tuple([:var1, :var2, :var3][[R1,R2,R3]])
    return quote
        row = $rowexpr
        push!(records.data, row)
    end
end

```

Arrays are easier for these kinds of manipulations.
