# Parametric type in an variable length Tuple?

**URL:** <https://discourse.julialang.org/t/parametric-type-in-an-variable-length-tuple/9109>\
**Category:** General Usage\
**Created:** [February 16, 2018, 9:38am UTC](https://discourse.julialang.org/t/parametric-type-in-an-variable-length-tuple/9109 "2018-02-16T09:38:06Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Raf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raf/32/3383_2.png) [@Raf](https://discourse.julialang.org/u/Raf)\
**Post date:** [February 16, 2018, 9:38am UTC](https://discourse.julialang.org/t/parametric-type-in-an-variable-length-tuple/9109/1 "2018-02-16T09:38:06Z")

</div>

I have a problem where I have a struct of parametrically typed functions contained in another struct - to make user selected functions available and type stable. This is fine for one set of functions, but I need N sets of functions! and all type stable. This is the naive approach, which doesn’t work unless all sets of functions are identical:

```julia
struct FunctionSet{A,B,C}
  func_a::A
  func_b::B
  func_c::C   
end

struct SomeStruct{N,A,B,C}
   function_sets::NTuple{N,FunctionSet{A,B,C}}
end

```

Any ideas on how to achieve this for multiple _varying_ FunctionSets? Is this possible?

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [February 16, 2018, 9:43am UTC](https://discourse.julialang.org/t/parametric-type-in-an-variable-length-tuple/9109/2 "2018-02-16T09:43:00Z")

</div>

I don’t think this is what you need, but I post it in case it is:

```julia
struct SomeStruct{N,FS<:FunctionSet}
   function_sets::NTuple{N,FS}
end

```

Otherwise, if your functionsets are different, then it is not a NTuple anymore. But you could do:

```julia
struct SomeStruct{T<:Tuple}
   function_sets::T
end

```

and check in the constructor that the elements of the `function_sets` are indeed `<:FunctionSet`.

---

<div class="post-metadata">

**Author:** ![juthohaegeman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juthohaegeman/32/8620_2.png) [@juthohaegeman](https://discourse.julialang.org/u/juthohaegeman)\
**Post date:** [February 16, 2018, 9:45am UTC](https://discourse.julialang.org/t/parametric-type-in-an-variable-length-tuple/9109/3 "2018-02-16T09:45:47Z")

</div>

```julia
struct SomeStruct{T<:Tuple{Vararg{<:FunctionSet}}}
    function_sets::T
end

```

---

<div class="post-metadata">

**Author:** ![Raf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raf/32/3383_2.png) [@Raf](https://discourse.julialang.org/u/Raf)\
**Post date:** [February 16, 2018, 9:48am UTC](https://discourse.julialang.org/t/parametric-type-in-an-variable-length-tuple/9109/4 "2018-02-16T09:48:34Z")

</div>

Your right it’s not an NTuple, just some grouping of N sets of different but interchangeable functions that are type stable.

---

<div class="post-metadata">

**Author:** ![juthohaegeman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juthohaegeman/32/8620_2.png) [@juthohaegeman](https://discourse.julialang.org/u/juthohaegeman)\
**Post date:** [February 16, 2018, 9:50am UTC](https://discourse.julialang.org/t/parametric-type-in-an-variable-length-tuple/9109/5 "2018-02-16T09:50:50Z")

</div>

And if you do want to have `N` available:

```julia
struct SomeStruct{N,T<:NTuple{N,FunctionSet}}
           function_sets::T
end
(::Type{SomeStruct})(a::NTuple{N,FunctionSet}) where {N} = SomeStruct{N,typeof(a)}(a)

```

---

<div class="post-metadata">

**Author:** ![Raf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raf/32/3383_2.png) [@Raf](https://discourse.julialang.org/u/Raf)\
**Post date:** [February 16, 2018, 10:29am UTC](https://discourse.julialang.org/t/parametric-type-in-an-variable-length-tuple/9109/6 "2018-02-16T10:29:43Z")

</div>

The first one is perfect, Thanks!

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [February 16, 2018, 10:46am UTC](https://discourse.julialang.org/t/parametric-type-in-an-variable-length-tuple/9109/7 "2018-02-16T10:46:56Z")

</div>

@Raf, can you mark the solution (with the tick)?

---

<div class="post-metadata">

**Author:** ![Raf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raf/32/3383_2.png) [@Raf](https://discourse.julialang.org/u/Raf)\
**Post date:** [February 19, 2018, 6:09am UTC](https://discourse.julialang.org/t/parametric-type-in-an-variable-length-tuple/9109/8 "2018-02-19T06:09:38Z")

</div>

Any ideas on how to loop over these using a for loop, maintaining type stability? I keep getting union types.

Recursion works great for this but tends to confuse people…

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [February 19, 2018, 6:53am UTC](https://discourse.julialang.org/t/parametric-type-in-an-variable-length-tuple/9109/9 "2018-02-19T06:53:23Z")

</div>

I’m pretty sure that you need recursion.

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [February 19, 2018, 7:33am UTC](https://discourse.julialang.org/t/parametric-type-in-an-variable-length-tuple/9109/10 "2018-02-19T07:33:26Z")

</div>

You could try [https://github.com/cstjean/Unrolled.jl](https://github.com/cstjean/Unrolled.jl)

---

<div class="post-metadata">

**Author:** ![Raf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raf/32/3383_2.png) [@Raf](https://discourse.julialang.org/u/Raf)\
**Post date:** [February 19, 2018, 2:07pm UTC](https://discourse.julialang.org/t/parametric-type-in-an-variable-length-tuple/9109/11 "2018-02-19T14:07:35Z")

</div>

Ok good to know. Unrolled looks interesting, but would you reccomend it as a dependency for core behaviour in a package?
