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

Making protocol inherit CaseIterable produces nonsensical errors

    XMLWordPrintable

    Details

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

      Description

      This program compiles as expected:

      protocol VectorIndex {}
      
      protocol Vector {
        associatedtype Element
        associatedtype Index: VectorIndex
        subscript(index: Index) -> Element { get set }
        init(_ indexToElementMapping: (Index) -> Element)
      }
      
      // Some methods for square matrices:
      extension Vector where
        Self.Element: Vector,
        Self.Index == Self.Element.Index,
        Self.Element.Element: BinaryFloatingPoint
      {
        func row(_ index: Index) -> Element { self[index] }
      
        func column(_ index: Index) -> Element { Element { self[$0][index] } }
      }
      

      But note that if we make VectorIndex inherit/refine CaseIterable, like so:

      protocol VectorIndex: CaseIterable {}
      

      The program will no longer compile, because:

      error: cannot convert value of type 'Self.Element.Index' (associated type of protocol 'Vector') to expected argument type 'Self.Index' (associated type of protocol 'Vector')
      

      ie, it seems like the constraint Self.Index == Self.Element.Index is now being ignored.

        Attachments

          Activity

            People

            Assignee:
            Unassigned Unassigned
            Reporter:
            jens Jens Persson
            Votes:
            1 Vote for this issue
            Watchers:
            2 Start watching this issue

              Dates

              Created:
              Updated: