0

Scenario:

there's a function which accepts a Variadic parameters with a restriction that there should be at least one argument passed:

func sum(_ number: Int, _ extraValues: Int...) -> Int {
  extraValues.reduce(0, +) + number
}

so the result is:

sum(1,2,3) // 6
sum(1) // 1
sum(12,3) // 15
sum() // ERROR: Missing argument for parameter #1 in call

This part works just fine. BUT!!

if I have another similar function:

func nestedSum(_ number: Int, _ extraValues: Int...) -> Int {
  extraValues.reduce(0, +) + number
} 

SO: how to call it from inside another function:

func sum(_ number: Int, _ extraValues: Int...) -> Int {
  extraValues.reduce(0, +) + number
  // HOW to call nestedSum from inside here ?
}

if I do:

func sum(_ number: Int, _ extraValues: Int...) -> Int {
  nestedSum(number, extraValues)
}

I'm having:

Cannot pass array of type 'Int...' as variadic arguments of type 'Int'

I can not even do:

func sum(_ number: Int, _ extraValues: Int...) -> Int {
  nestedSum2(extraValues)
}

func nestedSum2(_ extraValues: Int...) -> Int {
  extraValues.reduce(0, +)
}

with same:

Cannot pass array of type 'Int...' as variadic arguments of type 'Int'

HangarRash
  • 7,314
  • 5
  • 5
  • 32
Dmitrii Z
  • 145
  • 1
  • 5

0 Answers0