List<String> list = ["ham","eggs"];
Is it a linked list, an array list, a vector ... ? List<String> list = ["ham","eggs"];
Is it a linked list, an array list, a vector ... ?This is what the proposal says about that decision:
It is likely to be higher in space and time performance, in that the
compiler can choose efficient implementations for small collections
(such as empty collections and singletons). On the other hand, it is
less flexible, in that you cannot specify the implementation type.
From: http://mail.openjdk.java.net/pipermail/coin-dev/2009-March/0...Personally, my first instinct was to read the syntax as a form of initialisation, not something that would bar me from adding or deleting elements after the set is instantiated.
Sure, it's nice syntax, but I can come up with more scenarios where I'll want to initialize a collection than scenarios where I want to finalize one (well, excepting for the fact that this is java, where immutability is often considered a virtue).
If I get a variable of type List, I expect to be able to change the list.
The current API already uses this in a bunch of places to return immutable Lists. For example, the lists you get out of Collections.singletonList() or Collections.nCopies() are immutable.
That's a bit broken from an OO perspective (is the interface an interface or not?), but it's a better solution than the const v. non-const container mess that C++/STL has, at least.
The only way of allowing the programmer to do this, that I can think of for now, is by casting perhaps?
List<String> list = (LinkedList)["ham","eggs"];
Just an idea ...