I have a generic REST request:
struct Request<T> {…}
The T
is the return type of the request, for example:
struct Animal {…}
let animalRequest = Request<Animal>
let animal: Animal = sendRequest(animalRequest)
Now I would like to express that the generic type has to conform to Decodable
so that I can decode the JSON response from the server:
struct Request<T> where T: Decodable {…}
struct Animal: Decodable {…}
This makes sense and works – until I arrive at a request that has no response, a Request<Void>
. The compiler is not happy about that one:
Type 'Void' does not conform to protocol 'Decodable'
My mischevious attempt to solve this by adding the Decodable
conformance to Void
was quickly found out by the compiler:
extension Void: Decodable {…} // Error: Non-nominal type 'Void' cannot be extended
It feels right to have the request generic over the return type. Is there a way to make it work with Void
return types? (For example the requests that just create something on the server and don’t return anything.)