title: Generic method with lightweight Objective-C generics
body: <p>I have a generic HTTP request type:</p>
  
  <pre><code>@interface Request: NSObject
  
  - (id) parseResponseData: (NSData *) data;
  
  @end
  </code></pre>
  
  <p>I can annotate that to get some generics support:</p>
  
  <pre><code>@interface Request&lt;__covariant T&gt;
  
  - (T) parseResponseData: (NSData *) data;
  
  @end
  </code></pre>
  
  <p>Now Xcode correctly suggest the particular return types:</p>
  
  <pre><code>Request&lt;NSString*&gt; *request = [Request new];
  NSNumber *bar = [request parseResponseData:nil]; // I get a warning, nice!
  </code></pre>
  
  <p>I also have a <code>Webservice</code> class that loads a <code>Request</code> and returns a nullable instance of <code>T</code>:</p>
  
  <pre><code>@interface Webservice
  
  - (id) loadSynchronously: (Request *) request;
  
  @end
  </code></pre>
  
  <p>Is there a way to express, in Objective-C, that loading a <code>Request&lt;Foo*&gt;</code> returns a <code>Foo*</code> and not <code>id</code>?</p>
  
  <p>The best I came up with was inverting the call:</p>
  
  <pre><code>@interface Request&lt;__covariant T&gt;: NSObject
  
  - (T) parseResponseData: (NSData *) data;
  - (T) loadWithWebservice: (Webservice *) ws;
  
  @end
  </code></pre>
  
  <p>Which is an interesting hack, but not a good API. Maybe there’s a similar hack lurking somewhere that would work better?</p>