# Typed varargs

**URL:** <https://discourse.julialang.org/t/typed-varargs/118291>\
**Category:** General Usage\
**Tags:** type, varargs\
**Created:** [August 16, 2024, 8:30pm UTC](https://discourse.julialang.org/t/typed-varargs/118291 "2024-08-16T20:30:07Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nichola](https://avatars.discourse-cdn.com/v4/letter/n/f0a364/32.png) [@Nichola](https://discourse.julialang.org/u/Nichola)\
**Post date:** [August 16, 2024, 8:30pm UTC](https://discourse.julialang.org/t/typed-varargs/118291/1 "2024-08-16T20:30:07Z")

</div>

I would like to define a function with a variable number of typed arguments. In particular I would like to overload the + operator in this way

```julia
function Base.:+(a::MyType, (c, args...)) 

```

where c is a Number and args is of the form `String, Int, String, Int, String, Int ...` where `String, Int` come in pairs.

I’ve tried to at least enforcing that c is a number by doing

```julia
function Base.:+(a::MyType, (c::Number, args...)) 

```

and this does not compile

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [August 16, 2024, 8:34pm UTC](https://discourse.julialang.org/t/typed-varargs/118291/2 "2024-08-16T20:34:37Z")

</div>

don’t think you can do that:

```julia
help?> Vararg
search: Vararg

  Vararg{T,N}

...

Vararg{T,N} corresponds to exactly N elements of type T.

```

---

<div class="post-metadata">

**Author:** ![abraemer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abraemer/32/51403_2.png) [@abraemer](https://discourse.julialang.org/u/abraemer)\
**Post date:** [August 16, 2024, 8:47pm UTC](https://discourse.julialang.org/t/typed-varargs/118291/3 "2024-08-16T20:47:51Z")

</div>

@jling is correct you cannot do this directly. And probably it would be best to refactor that interface a bit as it looks a bit strange.

That being said you could do:

```julia
function Base.:+(a::MyType, rest::Tuple{Number, Varargs{Any}})

```

or (if you changed the interface to pass tuples of `String`, `Int`)

```julia
function Base.:+(a::MyType, rest::Tuple{Number, Varargs{Tuple{String,Int}}})

```

Alternatively, just don’t constrain the arguments, if you don’t plan on using dispatch anyways. You might get away without ambiguities in this case because I think `+` has no method defined for Tuples, but I might be wrong and can’t check right now.

However, as said in the beginning: This complicated type signature probably wants to become a proper type. I.e. define a struct to hold your `c::Int` and `String`s and `Int`s. That likely makes the code more legible and easier to understand.

---

<div class="post-metadata">

**Author:** ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)\
**Post date:** [August 16, 2024, 9:46pm UTC](https://discourse.julialang.org/t/typed-varargs/118291/4 "2024-08-16T21:46:00Z")

</div>

> [@Nichola](#):
>
> I’ve tried to at least enforcing that c is a number by doing
> 
> ```julia
> function Base.:+(a::MyType, (c::Number, args...)) 
> 
> ```
> 
> and this does not compile

The correct syntax is:

```julia
function Base.:+(a::MyType, (c, args...)::Tuple{Number,Vararg})

```

---

<div class="post-metadata">

**Author:** ![mikmoore](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mikmoore/32/31109_2.png) [@mikmoore](https://discourse.julialang.org/u/mikmoore)\
**Post date:** [August 16, 2024, 9:52pm UTC](https://discourse.julialang.org/t/typed-varargs/118291/5 "2024-08-16T21:52:17Z")

</div>

> [@Nichola](#):
>
> args is of the form `String, Int, String, Int, String, Int ...` where `String, Int` come in pairs.

You might find the `Pair` type (constructed via `"one" => 2` or `Pair("one", 2)` convenient for your pairs. You can constrain your `(c, args...)::Tuple{Number, Vararg{Pair}}` (or even get more specific with `Pair{String,Int}`) if you like, but as others have pointed out this is mostly useful if you plan to have the function behave differently with a different set of input types.
