Multiple Type Constraints in Swift

后端 未结 4 1498
暖寄归人
暖寄归人 2020-12-04 07:14

Let\'s say I have these protocols:

protocol SomeProtocol {

}

protocol SomeOtherProtocol {

}

Now, if I want a function that takes a gener

4条回答
  •  借酒劲吻你
    2020-12-04 07:42

    The evolution to Swift 3.0 brings some changes. Our two choices now look a little different.

    Using a where clause in Swift 3.0:

    The where clause has now moved to the end of a function signature to improve readability. So multiple protocol inheritance now looks like this:

    func someFunc(arg: T) where T:SomeProtocol, T:SomeOtherProtocol {
    
    }
    

    Using the protocol<> construct in Swift 3.0:

    Composition using the protocol<> construct is being deprecated. The earlier protocol now looks like this:

    func someFunc(arg: T) {
    
    }
    

    References.

    More info on the changes for where are here: https://github.com/apple/swift-evolution/blob/master/proposals/0081-move-where-expression.md

    And, more on the changes for the protocol<> construct are here: https://github.com/apple/swift-evolution/blob/master/proposals/0095-any-as-existential.md

提交回复
热议问题