Swift protocol forcing the Equatable protocol

Vincent Bernier

I have define 2 protocols. I need the first one (NameProtocol) to enforce the Equatable protocol. While the other class (BuilderProtocol) have a method that return the first one (NameProtocol).

public protocol NameProtocol : Equatable {
    var name: String { get }
}

public protocol BuilderProtocol {
    func build() -> NameProtocol? // Compiler error
    init()
}

The compiler error : "Protocol 'NameProtocol' can only be used as a generic constraint because it has Self or associated type requirements"

I need the object return by build() to return an object conforming to the NameProtocol and on which I can define ==

Is there a way I can make this work?

Thanks


If using a typealias in BuilderProtocol how can I make the array declaration work?

public protocol OtherRelatedProtocol {
    var allNames : Array<NameProtocol> { get }
}

Conclusion

I will remove the Equatable and implement an isEqual method.

public protocol NameProtocol {
    func isEqual(nameable: NameProtocol) -> Bool
    var name: String { get }
}
zneak

If you're familiar with Java or C#, Swift protocols are about halfway between generics and interfaces. One thing that you can do in a protocol, for instance, is this:

protocol Foo {
    func compareWith(foo: Self)
}

Classes that implement this protocol will have a method compareWith that accept an object of their own type (and not an object of type Foo).

This is what the compiler calls "Self or associated type requirements", and this is how Equatable is defined (it needs an operator== that accepts two Self operands). The downside of these protocols, though, is that you can only use them as generic constrains: you can't use them as an expression type.

The solution is to use generics. In this case, you'd make your ProtocolBuilder protocol generic, with a constraint that the type implements NameProtocol.

public protocol NameProtocol : Equatable {
    var name: String { get }
}

public protocol BuilderProtocol {
    typealias T: NameProtocol
    func build() -> T?
    init()
}

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Swift Equatable Protocol

分類Dev

Swift 2 Generic data structure not conforming to Equatable protocol

分類Dev

Why Hashable protocol forces variable to be comply with Equatable protocol?

分類Dev

Cast a protocol to another protocol Swift

分類Dev

Swift protocol for string interpolation

分類Dev

Swift downcasting and protocol variables

分類Dev

Swift Generic Protocol

分類Dev

Swift protocol inheritance

分類Dev

Swift : protocol extension and arrays

分類Dev

Swift - Default Implementation of protocol functions in another protocol

分類Dev

Swift 5 : Equatable with protocol을 구현하는 구조체에 일반 배열 작업 구현

分類Dev

Add class property to protocol in Swift

分類Dev

Swift KVO on an object conforming to a protocol

分類Dev

Swift - upcasting array of protocol to array of super protocol causes error

分類Dev

How to require an enum be defined in Swift Protocol

分類Dev

Class conforming to protocol as function parameter in Swift

分類Dev

@protocol(DelegateType)と同等のSwift

分類Dev

Swift Enum of custom types conform to hashable protocol

分類Dev

Swift protocol extension method dispatch with superclass and subclass

分類Dev

Swift 3 : AppDelegate does not conform to protocol GIDSignInDelegate

分類Dev

Adding property observer to Decodable protocol in Swift 4

分類Dev

Declare metatype that is a subclass and conforms to a protocol in Swift 4

分類Dev

Implement protocol partially in Objective C and partially in Swift

分類Dev

How to pass in protocol Enum array to function in Swift?

分類Dev

Swift Generic Protocol Class Type in Array

分類Dev

Swift - Type '' does not conform to protocol 'Hashable'

分類Dev

Swift multi-protocol conformance, compilation error

分類Dev

Swift func - Does not conform to protocol "Boolean Type"

分類Dev

Type conversion when using protocol in Swift

Related 関連記事

  1. 1

    Swift Equatable Protocol

  2. 2

    Swift 2 Generic data structure not conforming to Equatable protocol

  3. 3

    Why Hashable protocol forces variable to be comply with Equatable protocol?

  4. 4

    Cast a protocol to another protocol Swift

  5. 5

    Swift protocol for string interpolation

  6. 6

    Swift downcasting and protocol variables

  7. 7

    Swift Generic Protocol

  8. 8

    Swift protocol inheritance

  9. 9

    Swift : protocol extension and arrays

  10. 10

    Swift - Default Implementation of protocol functions in another protocol

  11. 11

    Swift 5 : Equatable with protocol을 구현하는 구조체에 일반 배열 작업 구현

  12. 12

    Add class property to protocol in Swift

  13. 13

    Swift KVO on an object conforming to a protocol

  14. 14

    Swift - upcasting array of protocol to array of super protocol causes error

  15. 15

    How to require an enum be defined in Swift Protocol

  16. 16

    Class conforming to protocol as function parameter in Swift

  17. 17

    @protocol(DelegateType)と同等のSwift

  18. 18

    Swift Enum of custom types conform to hashable protocol

  19. 19

    Swift protocol extension method dispatch with superclass and subclass

  20. 20

    Swift 3 : AppDelegate does not conform to protocol GIDSignInDelegate

  21. 21

    Adding property observer to Decodable protocol in Swift 4

  22. 22

    Declare metatype that is a subclass and conforms to a protocol in Swift 4

  23. 23

    Implement protocol partially in Objective C and partially in Swift

  24. 24

    How to pass in protocol Enum array to function in Swift?

  25. 25

    Swift Generic Protocol Class Type in Array

  26. 26

    Swift - Type '' does not conform to protocol 'Hashable'

  27. 27

    Swift multi-protocol conformance, compilation error

  28. 28

    Swift func - Does not conform to protocol "Boolean Type"

  29. 29

    Type conversion when using protocol in Swift

ホットタグ

アーカイブ