cyclical generics (try 2)

前端 未结 2 1627
甜味超标
甜味超标 2020-12-11 08:50

Second attempt at this question (the initial code wasn\'t enough to highlight the issue)

Here is the code that does not compile:

interface Player<         


        
2条回答
  •  悲哀的现实
    2020-12-11 09:15

    Mixing generics and raw types isn't going to work. If you need these interfaces to reference each other, they also need to reference themselves:

    interface Player, G extends Game>
    {
        R takeTurn(G game);
    }
    
    interface Game, P extends Player>
    {
        void play(P player);
    }
    

    Although this is looking rather hairbrained, and I'm not sure why you need it.

    Edit:

    I was able to implement your AbstractGame based on the above:

    abstract class AbstractGame>>
        implements Game, P>
    {
        public final void play(final P player)
        {
            final R value;
    
            value = player.takeTurn(this);
            turnTaken(value);
        }
    
        protected abstract void turnTaken(R value);
    }
    

    However I couldn't quite close the circuit with XGame and XPlayer:

    public class XGame
        extends AbstractGame //compile error on XPlayer
    {
    
        protected void turnTaken(Integer value) { }
    }
    
    public class XPlayer
        implements Player //compile error on XGame
    {
        @Override
        public Integer takeTurn(final XGame game)
        {
            return (42);
        }
    }
    

    The issue seems to be that each of the generic declarations of XGame and XPlayer needs the other to be correct. This is where your design is truly cyclical. If the compiler 'assumed' each was correct, it would in theory work. But it doesn't.

    Edit 2:

    How about this:

    interface Game>
    {
        void play(Player player);
    }
    
    interface Player>
    {
        R takeTurn(G game);
    }
    
    abstract class AbstractGame>
        implements Game
    {
        public final void play(final Player player)
        {
            final R value;
    
            value = player.takeTurn(self());
            turnTaken(value);
        }
    
        protected abstract G self();
    
        protected abstract void turnTaken(R value);
    }
    
    public final class XGame extends AbstractGame
    {
       protected XGame self() {
          return this;
       }
    
       protected void turnTaken(Integer value) { }
    }
    
    public class XPlayer implements Player
    {
        @Override
        public Integer takeTurn(final XGame game)
        {
           return (42);
        }
    }
    

    The key here was declaring an abstract method self() in AbstractGame that returns an instance of type G. Extending classes must resolve the inherited type parameter with their own type, and implement self() to return this. This is only suitable for internal code, since an extending class could easily lie, for example:

    public class EvilGame extends AbstractGame { ... }
    

    See my answer here and this post for more details on this pattern.

提交回复
热议问题