Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Not without dirty casting tricks. As you already noted Java doesn't support type level polymorphism (a.k.a. "higher kinded types" in Scala land).</p> <p>Here is one way: Say you want to implement a functor. You would like to write <code>Functor&lt;F&lt;A&gt;&gt;</code>, where <code>F</code> is e.g. a <code>List</code> or <code>Maybe</code>, but that doesn't work. But you can have a "base class" <code>Base&lt;X,Y&gt;</code> for higher-kinded stuff. <code>X</code> must be a "witness" for your real class like <code>List</code>. <code>Y</code> is the usual generic parameter. Now your functor becomes <code>Functor&lt;Base&lt;F,A&gt;&gt;</code>, but all classes that want to be used with this, need to implement <code>Base&lt;X,Y&gt;</code>:</p> <pre><code>class List&lt;A&gt; implements Base&lt;List.Witness,A&gt; { public class Witness{} ... } public interface Functor&lt;F&gt; { public &lt;A, B&gt; Base&lt;F, B&gt; map(F1&lt;A, B&gt; fn, Base&lt;F, A&gt; nestedA); } public class ListFunctor implements Functor&lt;List.Witness&gt; { public &lt;A, B&gt; Base&lt;List.Witness, B&gt; map(F1&lt;A, B&gt; fn, Base&lt;List.Witness, A&gt; nestedA) { ... } } </code></pre> <p>Of course the price to pay is that you get back a <code>Base&lt;List.Witness,B&gt;</code>, not a <code>List&lt;B&gt;</code>. This might be fine if you stay in that higher-kinded domain, and of course you can have conversion functions for convenience, but it's still not very nice. </p> <p>For an implementation see my not-so-serious project <a href="http://code.google.com/p/highj/" rel="noreferrer">highJ</a>. Note that I'm working on a completely rewritten and slightly more convenient version closer to the example above.</p> <p>For serious code, consider writing such stuff in Scala instead (or use Scalaz).</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload