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

Recursive conditional conformances

    XMLWordPrintable

    Details

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

      Description

      In the Swift 4.1 version toolchain snapshot from 19-12-2017  there's already some ongoing work with implementation of conditional conformances. In this version though, it is not possible to define somewhat recursive, safe-aware conditional conformances which would allow generic type to conform to a given protocol only if condition which regards the type itself is met.
      I don't really know if there's any kind of a specific term for such property in type theory, but basically what I was trying to achieve was:

       

      protocol Request {
      
          associatedtype ResponseType: Response
              where ResponseType.RequestType == Self
      }
      
      protocol Response {
      
          associatedtype RequestType: Request
              where RequestType.ResponseType == Self
      }
      
      struct BasicResponse<E: Request> { }
      
      extension BasicResponse: Response where E.ResponseType == BasicResponse {
      
          typealias RequestType = E
      }
      

       

      Currently trying to compile such code using the toolchain results in following: 

       

      main.swift:15:56: error: same-type constraint type 'BasicResponse<E>' does not conform to required protocol 'Response'
      extension BasicResponse: Response where E.ResponseType == BasicResponse {
                                                             ^
      main.swift:15:1: error: 'Response' requires the types 'BasicResponse<E>' and 'E.ResponseType' be equivalent
      extension BasicResponse: Response where E.ResponseType == BasicResponse {
      ^
      main.swift:15:1: note: requirement specified as 'Self' == 'Self.RequestType.ResponseType' [with Self = BasicResponse<E>]
      extension BasicResponse: Response where E.ResponseType == BasicResponse {
      ^

       

       

        Attachments

          Activity

            People

            • Assignee:
              Unassigned
              Reporter:
              osjup Damian Malarczyk
            • Votes:
              0 Vote for this issue
              Watchers:
              6 Start watching this issue

              Dates

              • Created:
                Updated: