Search code examples
swiftdefault-valueswift-protocols

How can I extend a Swift protocol with a default parameter when using generic types?


I have a protocol that is like

protocol QueryProtocol {
  associatedtype Data
}

protocol DataFetcher {
  func fetch<Query: QueryProtocol, Output>(
    query: Query,
    parser: (Query.Data) -> Output,
    completionHandler: (Output) -> Void
  )
}

I would like to extend the protocol and provide a default value for the parser to be the identity. So I tried

extension DataFetcher {
  func fetch<Query: QueryProtocol, Output>(
    query: Query,
    parser: (Query.Data) -> Output = { $0 }, // Cannot convert value of type 'Query.Data' to closure result type 'Output'
    completionHandler: (Output) -> Void
  ) {
    fetch(query: query, parser: parser, completionHandler: completionHandler)
  }
}

But the compiler fails with Cannot convert value of type 'Query.Data' to closure result type 'Output'

Is there any way I can specify that by default Query.Data = Output


Solution

  • Is there any way I can specify that by default Query.Data = Output

    No.

    But, you can define an overload which works only on Query.Data = Output:

    extension DataFetcher {
        func fetch<Query: QueryProtocol, Output>(
            query: Query,
            completionHandler: (Output) -> Void
        )
            where Query.Data == Output
        {
            fetch(query: query, parser: { $0 }, completionHandler: completionHandler)
        }
    }