title: Passing generic class type in java
body: <p>I'm trying to see if the template expression pattern can be imitated in Java, to do optimizations like loop fusion.</p>
  
  <p>As an example, I port the c++ classes found in this expression template example to java classes: <a href="https://en.wikipedia.org/wiki/Expression_templates#Motivation_and_example" rel="nofollow noreferrer">https://en.wikipedia.org/wiki/Expression_templates#Motivation_and_example</a></p>
  
  <p>First, a template class <code>VecExpression&lt;E&gt;</code> representing a vector expression. It uses a template parameter <code>E</code> and takes the class type of <code>E</code> as a constructor parameter. It then creates a private variable <code>thisAsE</code> set to <code>this</code> cast to the class type of <code>E</code></p>
  
  <pre><code>public abstract class VecExpression &lt;E&gt; {
      private VecExpression thisAsE;
  
      public VecExpression(Class&lt;E&gt; type) throws Exception {
          if(type.isInstance(this)) {
              thisAsE = (VecExpression)type.cast(this);   
          }
          else {
              throw new Exception("Class type must extend VecExpression");
          }
      }
  
      public double get(int i) {
          return thisAsE.get(i);
      }
  
      public int size() {
          return thisAsE.size();
      }
  }
  </code></pre>
  
  <p>Second, a class <code>Vec</code> extending <code>VecExpression&lt;Vec&gt;</code> which passes <code>Vec.class</code> into the super constructor and implements the <code>get()</code> and <code>size()</code> methods called in the <code>VecExpression&lt;E&gt;</code> class.</p>
  
  <pre><code>public class Vec extends VecExpression&lt;Vec&gt; {
  
      private double[] elems;
  
      public &lt;E&gt; Vec(VecExpression&lt;E&gt; expression) throws Exception {
          super(Vec.class);
          for(int i = 0; i &lt; expression.size(); ++i) {
              elems[i] = expression.get(i);
          }
      }
  
      public Vec(double[] elems) throws Exception {
          super(Vec.class);
          this.elems = elems;
      }
  
      public double get(int i) {
          return elems[i];
      }
  }
  </code></pre>
  
  <p>And third, a template class <code>VecSum&lt;E1, E2&gt;</code> which extends <code>VecExpression&lt;VecSum&lt;E1, E2&gt;</code>, and uses its <code>get()</code> method to return the sum of two <code>VecExpression&lt;E&gt;</code>s. The type is passed as an explicit parameter <code>Class&lt;VecSum&lt;E1, E2&gt;&gt; type</code>.</p>
  
  <pre><code>public class VecSum &lt;E1, E2&gt; extends VecExpression&lt;VecSum&lt;E1, E2&gt;&gt; {
  
      private VecExpression u;
      private VecExpression v;
  
      public VecSum(Class&lt;VecSum&lt;E1, E2&gt;&gt; type, VecExpression&lt;E1&gt; u, VecExpression&lt;E2&gt; v) throws Exception {
          super(type);
          if(u.size() != v.size()) {
              throw new Exception("Vectors must be of the same size");
          }
          this.u = u;
          this.v = v;
      }
  
      public double get(int i) {
          return u.get(i) + v.get(i);
      }
  
      public int size() {
          return v.size();
      }
  }
  </code></pre>
  
  <p>Finally, we use the expression template to generate a class that can add three vectors with a single pass through memory.</p>
  
  <pre><code>public class Main {
      public static void main(String[] args) throws Exception {
          Vec a = new Vec(new double[] {1, 2, 3});
          Vec b = new Vec(new double[] {1, 2, 3});
          Vec c = new Vec(new double[] {1, 2, 3});
  
          VecSum&lt;Vec, Vec&gt; ab = new VecSum&lt;Vec, Vec&gt;(VecSum&lt;Vec, Vec&gt;.class, a, b);
          VecSum&lt;VecSum&lt;Vec, Vec&gt;, Vec&gt; abc = new VecSum&lt;&gt;(VecSum&lt;VecSum&lt;Vec, Vec&gt;, Vec&gt;.class, ab, c);
      }
  }
  </code></pre>
  
  <p><strong>EDITED as per  Louis Wasserman's comment</strong></p>
  
  <p>However, the class types passed into the <code>VecSum</code> constructor don't work because the expression is trying to get a class from a parameterized type. Louis pointed out that implementations of a generic class don't compile to different classes like they do in c++. How would you pass their type, or is there another approach to the expression template pattern?</p>