title: Is binding to a TypeLiteral a good or bad practice in google guice
body: <p>Google guice uses <code>new TypeLiteral&lt;C&lt;T&gt;&gt;() {}</code> to overcome the fact that we cannot use <code>C&lt;T&gt;.class</code>. </p>
  
  <p>Now it is common to the following:</p>
  
  <pre><code>bind(new TypeLiteral&lt;C&lt;T&gt;&gt;() {}).to(MyCSubclassTypedToT.class);
  </code></pre>
  
  <p>Imagine a different scenario however. We have a generic interface, that we want to inject and the implementation we have is be provided by a generic class.</p>
  
  <p>Guice allows you to do this like this:</p>
  
  <pre><code>bind(new TypeLiteral&lt;MyGenericInterface&lt;T&gt;&gt;() {}).to(new TypeLiteral&lt;MyGenericClass&lt;T&gt;&gt;() {});
  </code></pre>
  
  <p>Another way of doing this would be to extend MyGenericClass like this:</p>
  
  <pre><code>MyTypedClass extends MyGenericClass&lt;T&gt;
  </code></pre>
  
  <p>and then bind it like this:</p>
  
  <pre><code>bind(MyGenericInterface&lt;T&gt;&gt;() {}).to(MyTypedClass.class);
  </code></pre>
  
  <p>If MyGenericInterface is injected a lot (albeit with different types), and every time I do inject it I use MyGenericClass, the latter approach leads to overly verbose code. Hence I'm leaning towards using the former.</p>
  
  <p>I would be very keen to hear other peoples opinion about the use of a TypeLiteral in the to clause of a guice binding. I'm afraid that I might a bit to short sited and thus don't see the pitfalls of this approach.</p>