# Grouping multiple varargs by type

**URL:** <https://discourse.julialang.org/t/grouping-multiple-varargs-by-type/7597>\
**Category:** General Usage\
**Created:** [December 7, 2017, 9:19am UTC](https://discourse.julialang.org/t/grouping-multiple-varargs-by-type/7597 "2017-12-07T09:19:35Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![louisponet](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/louisponet/32/2070_2.png) [@louisponet](https://discourse.julialang.org/u/louisponet)\
**Post date:** [December 7, 2017, 9:19am UTC](https://discourse.julialang.org/t/grouping-multiple-varargs-by-type/7597/1 "2017-12-07T09:19:35Z")

</div>

I was wondering if this is possible with the base julia features, something along the lines of `function foo(f_args:T... , i_args::R...) where {T<: AbstractFloat, R<: Int}` .

I understand that this is not necessary since there is the option to just run through the varargs and group them manually depending on their type, but it seems like a useful feature? This can probably be automated using a macro as well.

---

<div class="post-metadata">

**Author:** ![tim.holy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tim.holy/32/52_2.png) [@tim.holy](https://discourse.julialang.org/u/tim.holy)\
**Post date:** [December 7, 2017, 9:25am UTC](https://discourse.julialang.org/t/grouping-multiple-varargs-by-type/7597/2 "2017-12-07T09:25:11Z")

</div>

Currently there can be only one `Vararg` and it has to be last. You can, however, write this as

```julia
foo(f_args::NTuple{NT,T}, i_args::NTuple{NI,Int}) where {NT,NI,T<:AbstractFloat}

```

but the user has to group them manually.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [December 7, 2017, 9:34am UTC](https://discourse.julialang.org/t/grouping-multiple-varargs-by-type/7597/3 "2017-12-07T09:34:50Z")

</div>

Perhaps a generated function can sort them out and call the underlying grouped form @tim.holy suggested?

---

<div class="post-metadata">

**Author:** ![tim.holy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tim.holy/32/52_2.png) [@tim.holy](https://discourse.julialang.org/u/tim.holy)\
**Post date:** [December 7, 2017, 10:00am UTC](https://discourse.julialang.org/t/grouping-multiple-varargs-by-type/7597/4 "2017-12-07T10:00:29Z")

</div>

It doesn’t have to be a generated function, you can use “lispy tuple programming”:

```julia
groupargs(x::T, y...) where T = ((x, keepT(T, y...)...), groupargs(dropT(T, y...)...)...)
groupargs() = ()
# Keep leading arguments as long as they are of type T
keepT(::Type{T}, x::T, y...) where T = (x, keepT(T, y...)...)
keepT(::Type{T}, y...) where T = ()
# Drop leading arguments if they are of type T
dropT(::Type{T}, x::T, y...) where T = (dropT(T, y...)...)
dropT(::Type{T}, y...) where T = y

julia> groupargs(1,2,3,1.0,2.0)
((1, 2, 3), (1.0, 2.0))

```

Sometimes generated functions are the only way forward, but lispy programming is often faster to compile (at least for reasonably short tuples) and is friendlier for static compilation (because the compiler can reason in advance about what the code does, unlike with generated functions where the returned `Expr` could be arbitrary). With suitable `@inline` annotations there shouldn’t be a performance hit.
