Payload has to be something. It doesn't matter in this case what it is, but it still has to be known. The fact that the value is nil doesn't mean that you won't use the type itself in some way inside of the implementation, so Swift needs to know what it is.
The easiest way is to assign it to something already Encodable like String, adding the following overload:
func prepareRequest(for url: URL, method: String = "GET") -> AnyPublisher<URLRequest, Never> {
return prepareRequest(for: url, method: method, payload: Optional<String>.none)
}
You have to do this with overloads; there's no "default type parameter" feature in Swift. But any time you run into a limitation of default parameters, you can deal with it as an overload. Default parameters are just easier-to-type overloads after all.
If you're like me, having to pick a random type like String is annoying. The correct type here is Never, but Never doesn't conform to Encodable. It should (Never should conform to everything), but we can fix that by hand:
extension Never: Encodable {
public func encode(to encoder: Encoder) throws {
fatalError()
}
}
func prepareRequest(for url: URL, method: String = "GET") -> AnyPublisher<URLRequest, Never> {
return prepareRequest(for: url, method: method, payload: Optional<Never>.none)
}
There's no really tangible advantage doing it this way than using String, but it makes me feel better.