title: Java Generic Parameters across Dependent Java Types
body: <p>I have an interface Type that represents a metatype of data. The  parameter indicates the Java type that will be used to represent the data:</p>
  
  <pre><code>public interface Type&lt;J&gt; {
      boolean isAssignableFrom(@NotNull Type ts);
  
      /**
       * Gets the Java class of the representation of this type.
       */
      Class&lt;? extends J&gt; getValueType();
  }
  </code></pre>
  
  <p>I also have a value interface that represents an instance of that type:</p>
  
  <pre><code>public interface Value&lt;T extends Type&gt; {
      &lt;E&gt; E get();
  
      @NotNull
      T getType();
  }
  </code></pre>
  
  <p>What I'd like is to have the <code>Value</code> interface enforce the parameter declared on the <code>Type</code> interface. In other words, I want the <code>get()</code> method to declare that its return type MUST be within the bounds of <code>&lt;J&gt;</code>. But I cannot come up with any way to do this without adding <code>&lt;J&gt;</code> to the <code>Value</code> interface:</p>
  
  <pre><code>public interface Value&lt;J, T extends Type&lt;J&gt;&gt; {
      J get();
  
      @NotNull
      T getType();
  }
  </code></pre>
  
  <p>Is there any way to make <code>get()</code> reference its <code>Type</code>'s bounded parameter without adding it to the <code>Value</code> interface? It's making the code really a lot more complex than I'd like. </p>
  
  <p>So imagine <code>NumericType</code> which is a simple type to represent numbers in the system (there are more complex dynamic types that don't exist except at runtime, that's the joy of metaprogramming):</p>
  
  <pre><code>/** A static type representing some kind of number. */
  public class NumericType implements Type&lt;BigDecimal&gt;
  {
      public static final NumericType NUMERIC_TYPE = new NumericType();
  
  
      public boolean isAssignableFrom(@NotNull Type t)
      {
          // logic here
      }
  
      public Class&lt;BigDecimal&gt; getValueType() {
          return BigDecimal.class;
      }
  }
  </code></pre>
  
  <p>It tells callers the Java type used to represent this type in the system. If I declare a value of <code>NumericType</code>:</p>
  
  <pre><code>Value&lt;NumericType&gt; foo;
  </code></pre>
  
  <p>I'd like to be able to have compile-time safety that <code>get()</code> will <strong>always</strong> return a BigDecimal:</p>
  
  <pre><code>BigDecimal bar = foo.get();
  </code></pre>
  
  <p>That way, if I need to change the Java representation of a given Type (say, I want to make <code>NumericType&lt;Number&gt;</code> instead of <code>NumericType&lt;BigDecimal&gt;</code> the code above would fail at compile time. But how to I write <code>Value</code> such that its <code>get()</code> method declaration can reference the bounds of the <code>Type</code> bounded parameter? The <code>Value&lt;J, Type&lt;J&gt;&gt;</code> thing is really kludgy to read:</p>
  
  <pre><code>Value&lt;BigDecimal, NumericType&gt; foo;
  </code></pre>
  
  <p>I'm hoping there's a better way. </p>