简体   繁体   中英

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

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)
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM