Note that there are some explanatory texts on larger screens.

plurals
  1. POcyclical generics (try 2)
    text
    copied!<p>Second attempt at <a href="https://stackoverflow.com/questions/9422762/cyclical-generics">this</a> question (the initial code wasn't enough to highlight the issue)</p> <p>Here is the code that does not compile:</p> <pre><code>interface Player&lt;R, G extends Game&gt; { R takeTurn(G game); } interface Game&lt;P extends Player&gt; { void play(P player); } abstract class AbstractGame&lt;R, P extends Player&gt; implements Game&lt;P&gt; { public final void play(final P player) { final R value; value = player.takeTurn(this); turnTaken(value); } protected abstract void turnTaken(R value); } public class XPlayer implements Player&lt;Integer, XGame&gt; { @Override public Integer takeTurn(final XGame game) { return (42); } } public class XGame&lt;P extends Player&lt;Integer, XGame&gt;&gt; extends AbstractGame&lt;Integer, XPlayer&gt; { @Override protected void turnTaken(final Integer value) { System.out.println("value = " + value); } } public class Main { public static void main(final String[] argv) { final XPlayer player; final XGame game; player = new XPlayer(); game = new XGame(); game.play(player); } } </code></pre> <p>What I am running up against is trying to get the play method in the AbstractGame to compile. It seems that I have to run in circles with the Game and the Player adding generics to the extends/implements but for the life of me I cannot get it straight.</p> <p>The play method has to be final in the AbstractGame class, and there is no way to do casting, and I don't want to write another method like the turnTaken one to get it to work if I don't have to.</p> <p>EDIT: as requested here is the code that compiles, but needs the cast:</p> <pre><code>interface Player&lt;R, P extends Player&lt;R, P, G&gt;, G extends Game&lt;R, G, P&gt;&gt; { R takeTurn(G game); } interface Game&lt;R, G extends Game&lt;R, G, P&gt;, P extends Player&lt;R, P, G&gt;&gt; { void play(P player); } abstract class AbstractGame&lt;R, G extends Game&lt;R, G, P&gt;, P extends Player&lt;R, P, G&gt;&gt; implements Game&lt;R, G, P&gt; { public final void play(final P player) { final R value; value = player.takeTurn((G)this); turnTaken(value); } protected abstract void turnTaken(R value); } class XPlayer implements Player&lt;Integer, XPlayer, XGame&gt; { @Override public Integer takeTurn(final XGame game) { return (42); } } class XGame extends AbstractGame&lt;Integer, XGame, XPlayer&gt; { @Override protected void turnTaken(final Integer value) { System.out.println("value = " + value); } } class Main { public static void main(final String[] argv) { final XPlayer player; final XGame game; player = new XPlayer(); game = new XGame(); game.play(player); } } </code></pre>
 

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