Re: <? extends String>
On 4/23/13 11:33 AM, lipska the kat wrote:
On 23/04/13 17:58, Daniel Pitts wrote:
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.
Actually, in a way, List<? extends String> as a parameter to a method
might be considered as breaking encapsulation ... you are actually
exposing part of the internal workings of a component.
What about the internal workings? That it accepts a List of things it
can treat as Strings. I'm not seeing how that's breaking any encapsulation.
You're actually providing a guarantee that you won't be adding stuff to
this List (since it is a compiler error to do so).
In the General case.
Yes well this isn't the general case and I'm not convinced that writing
List<? extends String> as a parameter type because one day in the
distant future String might be mutable is a reason to do it, it's nasty
pointless and unnecessary complexity.
Sometimes if a general rule works for a special case, then it is easier
to follow that rule than to "optimize" for that special case.
The general rule here is
"if you aren't writing to it, use <? extends T>"
Just my opinion of course. :-)
Of course. Same goes for so much that is posted here by everyone. Some
are better opinions than others, but always on a case-by-case basis :-)