Re: <? extends String>
On 4/23/13 9:30 AM, lipska the kat wrote:
On 23/04/13 16:50, Daniel Pitts wrote:
On 4/23/13 8:37 AM, lipska the kat wrote:
On 23/04/13 14:55, Steven Simpson wrote:
On 23/04/13 11:31, Donkey Hottie wrote:
23.04.2013 12:48, Steven Simpson kirjoitti:
public static String generateWindowsArgument(List<? extends String>
args) {
<offtopic>java.lang.String is final, so nothing can extend it.
</offtopic>
It's a matter of habit drawn from the general principle that if I don't
need to modify the list, I don't impose the additional, unnecessary
constraint on the caller, regardless of the element type.
I don't understand this. In the first place List<? extends String> says
nothing about your ability to modify the list, you can add and remove
Strings to your hearts content,
Wrong.
List<? extends String> listOfStuff = new ArrayList<String>();
listOfStuff.add("foo"); // compiler error here!
erk, you're right, well I never, however it doesn't say anything about
the list outside of the method, how therefor does it avoid 'impose[ing]
the additional, unnecessary constraint on the caller' what constraint
does it avoid ?
In *this* case none. In the General case.
List<? extends MyFoo> fooList1;
List<MyFoo> fooList2;
fooList1 can be assigned an instance of List<MyFoo>, or List<MySubFoo>.
fooList2 can *only* be be assigned an instance of List<MyFoo>, not
List<MySubFoo>. This is an unnecessary constraint on the caller.
Again, in the case of "String", there isn't a difference. but that is a
special edge case which needn't change the general behavior.
secondly the only type you can pass as
argument to generateWindowsArgument is List<String> so the construct is
redundant anyway In fact, given that >>extends String<< is effectively
meaningless I'm surprised the compiler doesn't at least issue a warning.
In general, it is only meaningless in that String cannot be extended in
the current implementation of String. As Steven pointed out, that may
not be the case for future implementations, and the compiler will never
know that.
Well I reckon there's more chance of finding Elvis serving me with my
weekly fish and chips but OK, I suppose there's a rempote possibility it
might happen one day.
I suppose you get away with it because compile time type erasure gets
rid of any type arguments and as there can be only one type of argument
anyway a warning would be superfluous.
Erasure is a runtime phenomenon,
Erasure happens at compile time, generics are all about compile time
type checking, erasure removes type arguments at compile time after type
checking has occurred, the runtime system doesn't know anything about
type erasure or generics
http://docs.oracle.com/javase/tutorial/java/generics/erasure.html
[snip]
lipska