title: Type aliases for Java generics
body: <p>I have a fairly complicated set of generic classes in Java. For example, I have an interface</p>
  
  <pre><code>interface Doable&lt;X,Y&gt; {
    X doIt(Y y);
  }
  </code></pre>
  
  <p>and the implementation</p>
  
  <pre><code>class DoableImpl implements Doable&lt;Foo&lt;Bar&lt;Baz,Qux&gt;&gt;,Foo&lt;Bar&lt;Zot,Qux&gt;&gt;&gt; {
    Foo&lt;Bar&lt;Baz,Qux&gt;&gt; doIt(Foo&lt;Bar&lt;Zot,Qux&gt;&gt; fooBZQ) { ... }
  }
  </code></pre>
  
  <p>In the real implementation, <code>Doable</code> has quite a few methods and so <code>Foo&lt;Bar&lt;Baz,Qux&gt;&gt;</code>, etc., appear over and over again.
  (Believe it or not, the generic types are quite a bit more painful than this. I've simplified them for the example.)</p>
  
  <p>I'd like to simplify these, to save myself typing and to ease the strain on my eyes. What I'd like is to have a simple "type alias" for <code>Foo&lt;Bar&lt;Baz,Qux&gt;&gt;</code>, etc., say <code>FooBBQ</code> and <code>FooBZQ</code>. </p>
  
  <p>My current idea is to define wrapper classes:</p>
  
  <pre><code>class FooBBQ { 
    public static FooBBQ valueOf(Foo&lt;Bar&lt;Baz,Qux&gt;&gt; fooBBQ) { 
      return new FooBBQ(fooBBQ); 
    }
    private Foo&lt;Bar&lt;Baz,Qux&gt;&gt; fooBBQ;
    private FooBBQ(Foo&lt;Bar&lt;Baz,Qux&gt;&gt; fooBBQ) { 
      this.fooBBQ = fooBBQ; 
    }
    public Foo&lt;Bar&lt;Baz,Qux&gt;&gt; toGeneric() {
      return fooBBQ;
    }
  }
  
  class FooBZQ { /* pretty much the same... */ }
  
  class DoableImpl implements Doable&lt;FooBBQ,FooBZQ&gt; { 
    FooBBQ doIt(FooBZQ fooBZQ) { ... }
  }
  </code></pre>
  
  <p>This works well, but it has a few drawbacks:</p>
  
  <ol>
  <li>We need to define separate wrappers for each generic instance. The wrapper classes are short and stylized, but I can't figure out a way to macro-ize them. </li>
  <li><p>We have the translation overhead (conceptually, if not operationally) of calling <code>valueOf</code> and <code>toGeneric</code> to convert between <code>FooBBQ</code> and <code>Foo&lt;Bar&lt;Baz,Qux&gt;&gt;</code>. For example, if <code>doIt</code> calls into some library routine that expects a <code>Foo&lt;Bar&lt;Zot,Qux&gt;&gt;</code> (which the real implementation does), we end up with something like</p>
  
  <pre><code>return FooBBQ.valueOf( libraryCall( fooBZQ.toGeneric() ) )
  </code></pre>
  
  <p>where we would originally have had</p>
  
  <pre><code>return libraryCall(fooBZQ);
  </code></pre></li>
  </ol>
  
  <p>Is there some other way to get the "type alias" behavior I want here? Perhaps using some third-party macro toolset? Or do I need to accept that I'm going to have to do a lot of typing, one way (using the generic types in the implementation) or the other (writing wrappers for them)? Maybe having this many generic parameters flying around is just a bad idea and I need to re-think the problem?</p>
  
  <p>[UPDATE] OK, I'm banning any further "don't do that" answers. Take it as a given that <code>Foo&lt;Bar&lt;Baz,Qux&gt;&gt;</code> has genuine value in my problem domain (Pete Kirkham may be right that it has <em>enough</em> value to get a proper wrapper class with a descriptive name). But this is a programming problem; don't try to define the problem away.</p>