# Is it possible to generalize such function to arbitrary tuple size? I couldn't fin

**URL:** https://discourse.julialang.org/t/is-it-possible-to-generalize-such-function-to-arbitrary-tuple-size-i-couldnt-fin/55305
**Category:** General Usage
**Created:** [February 15, 2021, 10:13am UTC](https://discourse.julialang.org/t/is-it-possible-to-generalize-such-function-to-arbitrary-tuple-size-i-couldnt-fin/55305 "2021-02-15T10:13:41Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![BridgeBot](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bridgebot/32/21491_2.png) [@BridgeBot](https://discourse.julialang.org/u/BridgeBot)
#### Post date: [February 15, 2021, 10:13am UTC](https://discourse.julialang.org/t/is-it-possible-to-generalize-such-function-to-arbitrary-tuple-size-i-couldnt-fin/55305/1 "2021-02-15T10:13:41Z")

</div>

Is it possible to generalize such function to arbitrary tuple size? I couldn’t find relevant information in docs.

```julia
function from_vec(::Type{MyType{Tuple{T1, T2}}}, x) where {T1, T2}
	MyType((f(T1, x), f(T2, x)))
end

```

Note that the original poster on Slack cannot see your response here on Discourse. Consider _transcribing the appropriate answer back to Slack_, or pinging the poster here on Discourse so they can _follow this thread_.  
[(Original message :slack:)](https://julialang.slack.com/archives/C6A044SQH/p1613382820315100?thread_ts=1613382820.315100&cid=C6A044SQH) [(More Info)](https://github.com/JuliaCommunity/SlackBridge)

---

<div class="post-metadata">

### Author: ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)
#### Post date: [February 15, 2021, 10:38am UTC](https://discourse.julialang.org/t/is-it-possible-to-generalize-such-function-to-arbitrary-tuple-size-i-couldnt-fin/55305/2 "2021-02-15T10:38:01Z")

</div>

Presumably, you do not intend to use this with “arbitrary” tuple size. If you are expecting to need this with more than 5ish parameters (and probably more than 3 for most situations) – there is almost certainly a better approach. Defining each case is not much of a bother, and likely cleanest. Even if there were 8 parameters, this is still a good way to go.

```julia
function from_vec(::Type{MyType{Tuple{T1}}}, x) where {T1}
	MyType((f(T1, x),))
end
function from_vec(::Type{MyType{Tuple{T1, T2}}}, x) where {T1, T2}
	MyType((f(T1, x), f(T2, x)))
end
function from_vec(::Type{MyType{Tuple{T1, T2, T3}}}, x) where {T1, T2, T3}
	MyType((f(T1, x), f(T2, x), f(T3, x)))
end
# ...

```

---

<div class="post-metadata">

### Author: ![piever](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/piever/32/1815_2.png) [@piever](https://discourse.julialang.org/u/piever)
#### Post date: [February 15, 2021, 10:44am UTC](https://discourse.julialang.org/t/is-it-possible-to-generalize-such-function-to-arbitrary-tuple-size-i-couldnt-fin/55305/3 "2021-02-15T10:44:13Z")

</div>

There is also a trick to get this using `tuple_type_head` and `tuple_type_tail` from `Base`. I’m not sure how recommended it is, but it allows to do inferrable recursive definitions with tuple types. I’m removing `MyType` for simplicity.

```julia
julia> from_vec(::Type{Tuple{}}, x) = ()
from_vec (generic function with 1 methods)

julia> function from_vec(::Type{T}, x) where {T<:Tuple}
           S = Base.tuple_type_head(T)
           T1 = Base.tuple_type_tail(T)
           return (f(S, x), from_vec(T1, x)...)
       end
from_vec (generic function with 2 methods)

```

Then, for example,

```julia
julia> f(S, x) = S(x)
f (generic function with 1 method)

julia> from_vec(Tuple{Int, Float64}, 1)
(1, 1.0)

```
