Type mismatch: cannot convert from List


public class EntityRef<T extends Entity> {
...
}


public interface Entity{
	public List<EntityRef<?>> getForeignEntities();
...
}

so why type mismatch?


public class GameStatistik implements Entity{
	private List<EntityRef<PlayerData>> players;
	
	@Override
	public List<EntityRef<?>> getForeignEntities() {
		return players;  <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
	}
}

I don’t think wildcards work like that. Try changing it to


   @Override
   public List<EntityRef<PlayerData>> getForeignEntities() {
      return players;  //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
   }

[EDIT]:

If you want to store “anything” you can always store an Object. Though this means you need to check the returned objects with the instanceof operator for all the things.

This looks like it has something to do with covariance and type erasure. Here is a link that talks about something that may be similar.

This stackoverflow question might help as well.

[quote]I don’t think wildcards work like that. Try changing it to
[/quote]
dont work: The return type is incompatible with Entity.getForeignEntities()
Thats also confusing since that should be a case of covariant return type.

btw.: the folowing works:


public class GameStatistik implements Entity{
        private List<EntityRef<PlayerData>> players;
	@Override
	public List<EntityRef<?>> getForeignEntities() {
		return new ArrayList<EntityRef<?>>(players);
	}
}

I think you’d define the method in this way:


   public <T extends Entity> List<EntityRef<T>> getForeignEntities() {
      return new ArrayList<EntityRef<T>>(players);
   }