Say I'm writing a library to send / receive a given request / response.

I had the thought of doing something like:

protocol Request {
associatedtype ResponseType: Response
}

protocol Response {}

With this, you can do things like:

func request<T: Request>(_ r: T) -> T.ResponseType { }

It ends up being quite nice because you keep a strong association between the Request and Response types (and can do tricks w/ Codable).

I can't help but feeling there's a way to make this nicer… any ideas?

@b3ll It’s a way to go! A few years ago, I wrote a library to define REST API services and endpoints declaratively in such a way. https://github.com/futuredapp/FTAPIKit
GitHub - futuredapp/FTAPIKit: Declarative and generic REST API framework using Codable.

Declarative and generic REST API framework using Codable. - futuredapp/FTAPIKit

GitHub
@mkj this is oddly similar to what I'm doing now, glad to hear it works!