Uploaded image for project: 'Swift'
  1. Swift
  2. SR-1856

prefix() chained after sorted() and before map() confuses type inference in 3.0

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Resolved
    • Priority: Medium
    • Resolution: Done
    • Component/s: Compiler, Standard Library
    • Environment:

      macOS 10.11.5, Xcode 8 beta (8S128d) with Apple Swift version 3.0 (swiftlang-800.0.30 clang-800.0.24)

      Description

      In Swift 3.0 the following code:

      let datesWithCount: [(Date, Int)] = [(Date(), 1), (Date(), 2), (Date(), 3)]
      
      let dates: [Date] = datesWithCount.sorted {
          $0.0 < $1.0
      }.prefix(1).map {
          return $0.0
      }
      

      Causes the compiler to error with "Ambiguous use of 'prefix'", with the caret pointing to sorted.

      The same code compiles fine in Swift 2.2

      The only way to make it compile is either to force the result of prefix into an Array:

      let dates: [Date] = Array(datesWithCount.sorted {
          $0.0 < $1.0
      }.prefix(1)).map {
          return $0.0
      }
      

      ..or to split the map onto a separate line:

      let sortedDatesWithCount = datesWithCount.sorted {
          $0.0 < $1.0
      }.prefix(1)
      
      let mappedDates = sortedDatesWithCount.map {
          return $0.0
      }
      

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              rudkx Mark Lacey
              Reporter:
              mluisbrown Michael Brown
              Votes:
              4 Vote for this issue
              Watchers:
              5 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved: