Uploaded image for project: 'Swift'
  1. Swift
  2. SR-103

Protocol Extension: function's implementation cannot be overridden by a subclass

    Details

    • Type: Bug
    • Status: Open
    • Priority: Medium
    • Resolution: Unresolved
    • Component/s: Compiler
    • Labels:
      None
    • Environment:

      Mac OSX 10.11.1, Swift 2.2-dev

      Description

      A function(x') is not called that we thought If there is following conditions.

      1. A protocol(A) is defined a function(x).
      2. A has implement of x in the protocol extension.
      3. A class(B) is implement A, but doesn't have implement x.
      4. A subclass(C) is inheritance B, and have implement x(x').
      5. A instance of C typed A, and call x.
      // Defined protocol.
      protocol A {
          func a() -> Int
      }
      extension A {
          func a() -> Int {
              return 0
          }
      }
      
      // A class doesn't have implement of the function.
      class B: A {}
      
      class C: B {
          func a() -> Int {
              return 1
          }
      }
      
      // A class has implement of the function.
      class D: A {
          func a() -> Int {
              return 1
          }
      }
      
      class E: D {
          override func a() -> Int {
              return 2
          }
      }
      
      // Failure cases.
      B().a() // 0
      C().a() // 1
      (C() as A).a() // 0 # We thought return 1. 
      
      // Success cases.
      D().a() // 1
      (D() as A).a() // 1
      E().a() // 2
      (E() as A).a() // 2
      

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                slavapestov Slava Pestov
                Reporter:
                tarunon Nobuo Saito
              • Votes:
                16 Vote for this issue
                Watchers:
                30 Start watching this issue

                Dates

                • Created:
                  Updated: