1

This code was legal in Swift 1.1:

let arr = Array(1...100)
let sum = arr.reduce(0,+)

But in Swift 1.2 it is no longer legal. Instead, I am compelled to use the combine: parameter name explicitly:

let arr = Array(1...100)
let sum = arr.reduce(0,combine:+)

Why? I see no difference in their declarations — except for the new @noescape attribute (well explained here). But why should that change anything about the use of external parameter names?

Community
  • 1
  • 1
matt
  • 515,959
  • 87
  • 875
  • 1,141
  • I wonder why it compiled before, the Swift 1.2 behavior seems to be correct. – Martin R Apr 14 '15 at 07:23
  • @MartinR That possibility (maybe Swift 1.1 and before behavior was the bug) occurred to me as well. Maybe Swift 1.1 was thinking: "Well, this is the last parameter and it can be a trailing closure, so the parameter name is always omissible", while Swift 1.2 is thinking, "It is omissible only if it really _is_ a trailing closure." – matt Apr 14 '15 at 14:31

2 Answers2

3

I filed a bug report on this, and Apple replied that the change was intentional. In my view, being able to say reduce(0,+) was elegant and pithy, so the API for reduce should declare the external parameter name unnecessary. In Apple's stated view, the combine: external parameter name clarifies the purpose of the parameter. We have agreed to disagree.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • that's really interesting, seeing as they seemingly decided not to enforce that with the general `reduce` function (see my answer below). A little inconsistent. – Jarsen May 18 '15 at 15:51
  • I know - I told them that in my bug report. They basically blew me off. If you support my argument based on this inconsistency, I wish you'd file a bug too. – matt May 18 '15 at 15:52
0

You could always use the reduce function for sequences—they aren't forcing the combine: parameter name there.

reduce(1...5, 0, +)
Jarsen
  • 7,432
  • 6
  • 27
  • 26
  • True but irrelevant. I'm specifically asking about _this_ `reduce`, not _that_ `reduce`. – matt May 18 '15 at 15:54