2

I was doing some exercises on Xcode 9 beta 2 Swift 4 from this article (https://www.uraimo.com/2016/01/06/10-Swift-One-Liners-To-Impress-Your-Friends/) when I came across an error while doing item no. 6:

extension Sequence{
   typealias Element = Self.Iterator.Element 

   func partitionBy(fu: (Element)->Bool)->([Element],[Element]){
       var first=[Element]() 
       var second=[Element]() 
       for el in self {
          if fu(el) { 
             first.append(el) 
          }else{ 
             second.append(el) 
          } 
       } 
       return (first,second) 
   } 
}

Xcode 9 was throwing an error in the following lines:

   var first=[Element]() 
   var second=[Element]() 

The full error is below:

error: Swift-Playground.playground:6:29: error: cannot call value of non-function type '[Self.Element.Type]'
        var second=[Element]()

The error persists even if I remove the typealias and use the full Self.Iterator.Element type.

This code works perfectly on Swift 3. I see no reason why it shouldn't work on Swift 4. Can someone help me out if it's a change in Swift 4 in terms of handling associated types and if so, what's the alternative to instantiate the array.

mj_jimenez
  • 443
  • 2
  • 5

1 Answers1

3

In Swift 4, protocol Sequence already defines the

associatedtype Element where Self.Element == Self.Iterator.Element

so you can just remove the

typealias Element = Self.Iterator.Element

to make it compile.

Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382