HappyEngineer
asked on
List<? extends ForumGroup> forumGroups cannot be added to
Let's say I have code like this:
List<? extends ForumGroup> forumGroups = null;
if(primaryForumGroup==null && forum!=null) {
forumGroups = forum.getForumGroups();
}
the method forum.getForumGroups() returns a value of type
List<? extends ForumGroup>
so that needs to be the type for that variable.
However, in this code I actually need to do this:
List<? extends ForumGroup> forumGroups = null;
if(primaryForumGroup!=null ) {
forumGroups = new LinkedList<ForumGroup>();
forumGroups.add(primaryFor umGroup);
}
if(primaryForumGroup==null && forum!=null) {
forumGroups = forum.getForumGroups();
}
That code won't compile. The problem is that forum.getForumGroups() returns
List<? extends ForumGroup)
but it's not possible to call the add method on a List that uses a "?" capture type.
I could obviously just have a List<ForumGroup> and copy values over from the List<? extends ForumGroup>, but I was hoping there was a more elegant way to do this.
Is there a type declaration for the forumGroups variable that would allow me to do this?
List<? extends ForumGroup> forumGroups = null;
if(primaryForumGroup==null
forumGroups = forum.getForumGroups();
}
the method forum.getForumGroups() returns a value of type
List<? extends ForumGroup>
so that needs to be the type for that variable.
However, in this code I actually need to do this:
List<? extends ForumGroup> forumGroups = null;
if(primaryForumGroup!=null
forumGroups = new LinkedList<ForumGroup>();
forumGroups.add(primaryFor
}
if(primaryForumGroup==null
forumGroups = forum.getForumGroups();
}
That code won't compile. The problem is that forum.getForumGroups() returns
List<? extends ForumGroup)
but it's not possible to call the add method on a List that uses a "?" capture type.
I could obviously just have a List<ForumGroup> and copy values over from the List<? extends ForumGroup>, but I was hoping there was a more elegant way to do this.
Is there a type declaration for the forumGroups variable that would allow me to do this?
what type is forum?
ASKER
That won't work because forum.getForumGroups() is defined as:
public List<? extends ForumGroup> getForumGroups();
and you can't assign that result to List<ForumGroup>. I find that an irritating behavior because, while technically correct, it's a detail that the compiler/jvm should deal with.
I'll make it a little clearer. The code does this:
T t = someVal;
List<T> vals = null;
if(someCondition) {
vals = new LinkedList<T>();
vals.add(t);
}
if(someOtherCondition) {
// obj.getVals() returns a value of type List<? extends T>. This assignment won't compile because assigning
// List<? extends T> to a variable of type List<T> is not allowed.
vals = obj.getVals();
}
public List<? extends ForumGroup> getForumGroups();
and you can't assign that result to List<ForumGroup>. I find that an irritating behavior because, while technically correct, it's a detail that the compiler/jvm should deal with.
I'll make it a little clearer. The code does this:
T t = someVal;
List<T> vals = null;
if(someCondition) {
vals = new LinkedList<T>();
vals.add(t);
}
if(someOtherCondition) {
// obj.getVals() returns a value of type List<? extends T>. This assignment won't compile because assigning
// List<? extends T> to a variable of type List<T> is not allowed.
vals = obj.getVals();
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Huh. I thought that doing that cast was illegal, so I didn't even try. I just tried now and it works. Great!
List<ForumGroup> forumGroups = null;
if(primaryForumGroup!=null
forumGroups = new LinkedList<ForumGroup>();
forumGroups.add(primaryFor
}
if(primaryForumGroup==null
forumGroups = forum.getForumGroups();
}