Extend array types using where clause in Swift

后端 未结 7 1960
青春惊慌失措
青春惊慌失措 2020-11-28 07:35

I\'d like to use the Accelerate framework to extend [Float] and [Double] but each of these requires a different implementation.

I tried the obvious:

         


        
相关标签:
7条回答
  • 2020-11-28 08:07

    If you want to extend only array with specific type. You should extend _ArrayType protocol.

    extension _ArrayType where Generator.Element == Int {
    
       func doSomething() {
           ... 
       }
    }
    

    If you extend Array you can only make sure your element is conformed some protocol else. i.e:

    extension Array where Element: Equatable {
    
       func doSomething() {
           ... 
       }
    }
    

    Updated: With Swift 3.1 https://github.com/apple/swift/blob/master/CHANGELOG.md

    extension Array where Element == Int {
    
       func doSomething() {
           ... 
       }
    }
    
    0 讨论(0)
提交回复
热议问题