Why Swift throws error when using optional param in closure func?

后端 未结 1 1805
囚心锁ツ
囚心锁ツ 2020-12-22 07:20

Can someone explain why the swift compiler complains about the \"offset:\" parameter which is optional anyway?

If I pass zero as param incrementByFive(0)

相关标签:
1条回答
  • 2020-12-22 08:00

    Only functions can have default parameters in Swift, but not closures.

    The closure returned from makeIncrementer() has the type (Int?) -> Int, i.e. it takes one argument of type Int?, and that must be provided when calling the closure.

    Compare also SR-531 Allow default parameters in closure parameters, in particular the comment

    Doesn't really make sense, if you need this behavior you should be using a function. Closures are usually called anonymously with their arguments all provided, so there is no need for default parameter values.

    0 讨论(0)
提交回复
热议问题