# Type parameter for each struct in \`Vararg\`

**URL:** <https://discourse.julialang.org/t/type-parameter-for-each-struct-in-vararg/80142>\
**Category:** General Usage\
**Tags:** question\
**Created:** [April 27, 2022, 2:27pm UTC](https://discourse.julialang.org/t/type-parameter-for-each-struct-in-vararg/80142 "2022-04-27T14:27:55Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![tictaccat](https://avatars.discourse-cdn.com/v4/letter/t/9f8e36/32.png) [@tictaccat](https://discourse.julialang.org/u/tictaccat)\
**Post date:** [April 27, 2022, 2:27pm UTC](https://discourse.julialang.org/t/type-parameter-for-each-struct-in-vararg/80142/1 "2022-04-27T14:27:55Z")

</div>

I have the function `f(args::Vararg{MyStruct{T, V}}) where {T,V}`. However, I would actually like to allow the `V`’s to be completely different (possibly not even promotable) for different elements of the `Vararg`, but I would still like to extract the type parameter for each.

For two arguments, this would look like `f(arg1::MyStruct{T, V1}, arg2::MyStruct{T, V2}) where {T,V1,V2}`. Is there any way to generalize this construct for a `Vararg`?

---

<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:** [April 27, 2022, 2:33pm UTC](https://discourse.julialang.org/t/type-parameter-for-each-struct-in-vararg/80142/2 "2022-04-27T14:33:24Z")

</div>

I think the nicest way to do this is to write your own function to get the `V` parameter from a `MyStruct` and then call that yourself, rather than going through the `where` machinery:

```julia
julia> struct MyStruct{T, V}
           x::T
           y::V
       end

julia> v_type(::MyStruct{T, V}) where {T, V} = V
v_type (generic function with 1 method)

julia> function f(args::Vararg{<:MyStruct})
         println("V types: ", v_type.(args))
       end
f (generic function with 1 method)

julia> f(MyStruct(1, 2.0), MyStruct(1, "hello"))
V types: (Float64, String)

```

This is just like using `eltype` for an `AbstractArray`. Sometimes it’s convenient to write code like:

```julia
function foo(x::AbstractVector{T}) where {T}
  println("got a vector of ", T)
end

```

But it’s just as efficient and sometimes more convenient to write:

```julia
function foo(x::AbstractVector)
  println("got a vector of ", eltype(x))
end

```

---

<div class="post-metadata">

**Author:** ![hamblingreg52](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hamblingreg52/32/23956_2.png) [@hamblingreg52](https://discourse.julialang.org/u/hamblingreg52)\
**Post date:** [May 13, 2022, 6:37am UTC](https://discourse.julialang.org/t/type-parameter-for-each-struct-in-vararg/80142/3 "2022-05-13T06:37:58Z")

</div>

Thanks, @rdeits I have tried your suggested method which has proven very effective. I am very happy to become this community. Hope to find more such educational information in the future.
