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

Can not use protocol to fulfill associatedtype requirement where associatedType has protocol constraint

    Details

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

      Description

      The following code sample does not compile.

      protocol Bar {   
      }
      
      protocol Buz: Bar {   
      }
      
      protocol Foo {
          associatedtype SomeType: Bar
          func someOperation(someType: SomeType)
      }
      
      struct Quack: Foo {
          func someOperation(someType: Buz) {
          }
      }
      
      

      It throws error:

      Inferred type 'Buz' (by matching requirement 'someOperation') is invalid: does not conform to 'Bar'
      

      I am not sure if we are only allowed to use a concrete type for associatedtype with protocol constraint. I don't see any reason for this restriction. If we remove the protocol constraint from associatedtype, then it compiles with no error.

      Update 2016/12/12: I am using below workaround to tackle above bug. This seems more logical and makes me feel like its not a bug.

      struct Quack<T>: Foo where T: Buz {
          func someOperation(someType: T) {
          }
      }
      
      // Usage
      struct SomeBuz: Buz {}
      let quack: Quack<SomeBuz> = Quack<SomeBuz>()
      quack.someOperation(SomeBuz())
      

      The reason this makes more sense to me is that the compiler now has more context on the actual type.

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                Unassigned
                Reporter:
                vishal.panwar Vishal Singh
              • Votes:
                10 Vote for this issue
                Watchers:
                13 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: