Swift 2.0: Protocol extensions: Two protocols with the same function signature compile error

后端 未结 1 2045
温柔的废话
温柔的废话 2020-12-06 00:10

Given the two protocols and their extensions:

protocol FirstDelegate {
    func someFunc()
}

protocol SecondDelegate {
    func someFunc()
}

extension Firs         


        
相关标签:
1条回答
  • 2020-12-06 00:34

    A protocol defines requirements (methods, properties, ...) for a conformant type.

    protocol FirstDelegate {
        func someFunc()
    }
    
    protocol SecondDelegate {
        func someFunc()
    }
    

    defines two protocols with the same required method someFunc(). A conformant type must implement this method:

    class SomeClass: FirstDelegate, SecondDelegate {
        func someFunc() {
            print("SomeClass implementation")
        }
    }
    

    A protocol extension provides method and property implementations to conformant types. A special case of a protocol extension is a default implementation, which is what you defined here:

    extension FirstDelegate {
        func someFunc() {
            print("First delegate")
        }
    }
    

    It defines a default implementation of someFunc() for all types conforming to FirstDelegate. Since this is the only required method of that protocol, a conforming class need not define the method at all:

    class SomeClass: FirstDelegate {
    
    }
    
    SomeClass().someFunc() // Output: First delegate
    

    But if the class provides its own implementation then that will be used:

    class SomeClass: FirstDelegate {
        func someFunc() {
            print("SomeClass implementation")
        }
    }
    
    SomeClass().someFunc() // Output: SomeClass implementation
    

    In your case, you have defined default implementations of someFunc() for both protocols:

    extension FirstDelegate {
        func someFunc() {
            print("First delegate")
        }
    }
    
    extension SecondDelegate {
        func someFunc() {
            print("Second delegate")
        }
    }
    

    A class can still conform to both protocols if it provides its own implementation of the required method:

    class SomeClass: FirstDelegate, SecondDelegate {
        func someFunc() {
            print("SomeClass implementation")
        }
    }
    

    But the class cannot conform by using the default implementation

    class SomeClass: FirstDelegate, SecondDelegate {
    
    }
    

    for both protocols because there is a conflict. It is unspecified which default implementation should be used, and that's why the compiler complains.

    Actually the class now conforms to none of the protocols. This can be seen in the full compiler log in the Report navigator:

    main.swift:24:7: error: type 'SomeClass' does not conform to protocol 'FirstDelegate'
    class SomeClass: FirstDelegate, SecondDelegate {
          ^
    main.swift:5:10: note: multiple matching functions named 'someFunc()' with type '() -> ()'
        func someFunc()
             ^
    main.swift:19:10: note: candidate exactly matches
        func someFunc() {
             ^
    main.swift:13:10: note: candidate exactly matches
        func someFunc() {
             ^
    main.swift:24:7: error: type 'SomeClass' does not conform to protocol 'SecondDelegate'
    class SomeClass: FirstDelegate, SecondDelegate {
          ^
    main.swift:9:10: note: multiple matching functions named 'someFunc()' with type '() -> ()'
        func someFunc()
             ^
    main.swift:19:10: note: candidate exactly matches
        func someFunc() {
             ^
    main.swift:13:10: note: candidate exactly matches
        func someFunc() {
             ^
    
    0 讨论(0)
提交回复
热议问题