"rschmid-" <rschmid-> writes:
> Niklas Matthies wrote:
>> List<? extends SourceMatch> sourceList =
>> sourceMatchService.getMatchingSources(args);
>
> Great! Except now;
>
> sourceList = new ArrayList<SourceMatch>();
> sourceList.add(new SourceMatch());
>
> throws an error.
As it should. ArrayList<SourceMatch> is not assignable to
List<? extends SourceMatch>.
You can do
sourceList = new ArrayList<? extends SourceMatch>();
but you can't add a SourceMatch to that (or anything at all).
Remember, a List<SourceMatch> and a List<CMatch> are not assignable
to each other. The former allows you to put SourceMatch'es into it,
the latter doesn't. And the latter guarantees that what you take
out of it is a CMatch, the former doesn't.
If you want a variable to hold both of the above list types, it
needs to be something like List<? extends SourceMatch> (which is
a supertype of both). However, you cannot add elements to that list,
since it might be both a List<CMatch> or a List<DMatch>, which cannot
contain the same elements.
Instead, you could just let the getMatchingSources return a
List<SourceMatch>.
/L
--
Lasse Reichstein Nielsen -
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'