Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Variadic parameters and defaulted parameters

Tags:

swift

I have this function:

func sum(#startingValue:Int, additionalValue:Int = 77, values:Int...) -> Int {
    var total:Int = startingValue + additionalValue
    for v in values {
        total += v
    }

    return total
}

Is there any way I can call it without specifying value for additionalValue argument?

What I want is something like this:

sum(startingValue:10, 1, 2, 3, 4, 5, 6, 7)
like image 833
AndrewShmig Avatar asked Jun 09 '14 16:06

AndrewShmig


People also ask

What is Variadic parameters in Swift?

In Swift, variadic parameters are the special type of parameters available in the function. It is used to accept zero or more values of the same type in the function. It is also used when the input value of the parameter is varied at the time when the function is called.

What are the parameters that are default values?

A parameter with a default value, is often known as an "optional parameter". From the example above, country is an optional parameter and "Norway" is the default value.

What are Variadic parameters in Python?

Variadic parameters (Variable Length argument) are Python's solution to that problem. A Variadic Parameter accepts arbitrary arguments and collects them into a data structure without raising an error for unmatched parameters numbers.

What is Variadic parameters in Golang?

A variadic function is a function that accepts a variable number of arguments. In Golang, it is possible to pass a varying number of arguments of the same type as referenced in the function signature.


1 Answers

Although this may seem like a weird work around, it does work, you can use method overloading:

// Calling this will result in using the default value
func sum(#startingValue:Int, values:Int...) -> Int {
    return sum(startingValue: startingValue, values);
}

// Calling this will use whatever value you specified
func sum(#startingValue:Int, #additionalValue:Int, values:Int...) -> Int {
    return sum(startingValue: startingValue, additionalValue: additionalValue, values);
}

// The real function where you can set your default value
func sum(#startingValue:Int, additionalValue:Int = 77, values:Int[]) -> Int {
    var total:Int = startingValue + additionalValue
    for v in values {
        total += v
    }

    return total
}

// You can then call it either of these two ways:
// This way uses will use the value 77 for additional value
sum(startingValue:10, 1, 2, 3, 4, 5, 6, 7) // = 115

// This way sets additionalValue to the value of 1
sum(startingValue:10, additionalValue: 1, 2, 3, 4, 5, 6, 7) // = 38

To be honest, I am not entirely sure why your first solution did not work automatically, in the docs I found this:

If your function has one or more parameters with a default value, and also has a variadic parameter, place the variadic parameter after all the defaulted parameters at the very end of the list.

But was unable to make it work, maybe a bug? I would guess it is supposed to work the same way I showed you. If you specify additionalValue it will use it, otherwise it will use the default. So maybe it will work automatically in the near future (making this solution irrelevant)?

Original Answer

The solution below works if you solely want to cease using the word additionalValue while calling the function but it still assigns additionalValue an argument (not what the OP was looking for).

Put an underscore in front of additionalValue:

func sum(#startingValue:Int, _ additionalValue:Int = 77, values:Int...) -> Int {
    // ...
}

Then you can call it how you want without warnings:

sum(startingValue:10, 1, 2, 3, 4, 5, 6, 7)

In this case additionalValue automatically equals the second parameter, so it would be equal to 1

like image 85
Firo Avatar answered Jan 02 '23 11:01

Firo