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

Protocol composition types should be covariant with composing protocols for protocol conformance

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Resolved
    • Priority: Medium
    • Resolution: Duplicate
    • Component/s: Compiler
    • Labels:
      None
    • Environment:

      Mac OS 10.11.1 (15B42)
      Xcode 7.2 (7C68)

      Description

      The type protocol<A,B> should be treated as covariant with the type A since all types conforming to protocol<A,B> conform to A.

      This works for function calling:

      protocol A {}
      protocol B {}
      struct Foo: A, B {}
      func bar(a: A) {}
      let composite: protocol<A,B> = Foo()
      bar(composite)
      

      However, it does not work for protocol conformance. Given A and B above, I expect the following to compile, but it does not:

      protocol HasA {
          var a: A { get }
      }
      struct Baz: HasA {
          var a: protocol<A,B>
      }
      

      Similarly, I expect function return values to work as well, but this does not compile:

      protocol Baffer {
          func baf() -> A
      }
      struct Baz: Baffer {
          func baf() -> protocol<A,B> { return Foo() }
      }
      

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                Unassigned
                Reporter:
                ianterrell Ian Terrell
              • Votes:
                0 Vote for this issue
                Watchers:
                3 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: