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

Unable to rename protocol associatedtype through inheritance

    XMLWordPrintable

    Details

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

      MacOS 10.15, XCode 11.2.

      Description

      If I have protocol Old with associatedtype OldType, I may want to define protocol New with associatedtype NewType that inherits Old and provides NewType for OldType. My first attempt at this fails unexpectedly:

      protocol Old {
          associatedtype OldType
      }
      protocol New: Old {
          associatedtype NewType
          typealias OldType = NewType // Warning
      }
      class Impl: New { // Error
          typealias NewType = String
      }
      

      There's both a warning:

      Typealias overriding associated type 'OldType' from protocol 'Old' is better expressed as same-type constraint on the protocol

      and Impl fails to compile with

      Type 'Impl' does not conform to protocol 'New' Type 'Impl' does not conform to protocol 'Old'

      This appears to work:

      protocol Old {
          associatedtype OldType
      }
      protocol New: Old where OldType == NewType {
          associatedtype NewType
      }
      extension New {
          typealias OldType = NewType // Warning
      }
      class Impl: New {
          typealias NewType = String
      }
      

      except there is the same warning as above in the extension and the extension itself should be unnecessary. That makes me think the first approach should work, and the warning is incorrect since there is already a where clause on the protocol.

       

       

        Attachments

          Activity

            People

            Assignee:
            Unassigned Unassigned
            Reporter:
            ral Richard Low
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Dates

              Created:
              Updated: