What are the reasons to use multiple variadic arguments?

Recently, I discovered that in Swift, it is possible to use more than one variadic argument in a function. Interestingly, this capability wasn't present initially; it was introduced through the SE-0284 proposal.

I am curious about possible use cases where it is meaningful to use two or more varargs instead of passing at least one of them as an array, for example.

I attempted to search through various real codebases from different projects but found nothing, despite this feature being available for at least three years.

I've reviewed the arguments for this feature presented in the initial proposal. However, I found only two, and the first one seems rather artificial to me.

Hence, I'm curious if there are any other examples or instances where this feature has proven particularly useful or efficient?"

2 Likes

One example I can think of:

// Give me at least one Int value
//
func foo (u: Int, _ v: Int...) {
    ...
}

Edit:

// Give me at least one Int value (quite silly)
//
func foo (u: Int, _ v: Int..., w: Int...) {
    ...
}

u in your example is not variadic, though.

1 Like

Sorry, I should have read the OP more carefully. Will edit my example.

I'm not sure how the second option can be useful to pass at least one Int value, especially since I completely don't understand the use of the second variadic here. You will still need to specify it (..., w: 1, 2, 3, ...)) in the calling code.

Not sure what the use case it but it enables things like

func f(i: Int, _ is: Int..., s: String, _ ss: String...)

for “at least one int and at least one string”

I concocted that example to illustrate the use case "pass at least one Int argument." :slight_smile:

// Give me at least one Int
func foo (u: Int, _ v:Int..., w: Int...) {
    print ("-->", u, v, w)
}

foo (u: 3)
// prints --> 3 [] []

The last parameter doesn't have be Int, and the corresponding argument may be omitted.

This isn't the best regex in the world and GitHub isn't all encompassing but it seems to be something not used much. At least the feature is complete enough to allow that kind of API design if one desires :person_shrugging:

Rough GitHub Regex Search