Neatly decoding the absence of data

Personally, I'd add an overload of the request function that omits the generic parameter and associated Decodable constraint:

func request(url: URL, callback: (Result<Void, Error>) -> Void))

(alternatively, drop Result and pass an Error? to the callback)

The downside to this is that it can lead to ambiguities during type inference/overload resolution, but since you're not using the generic parameter elsewhere in your signature, I'm guessing you have to manually write the types in the callback anyway. e.g:

request(url: URL(string: "blahblah")!) { (response: Result<String, Error>) in
  // ...
}

So if that's true, it would fit right in to your existing model and there shouldn't be any ambiguity.

1 Like