JEP 186: Collection Literals
Tom Hawtin
tom.hawtin at oracle.com
Thu Jan 23 16:17:23 PST 2014
On 23/01/2014 20:53, Zhong Yu wrote:
> On Thu, Jan 23, 2014 at 12:11 PM, Tom Hawtin <tom.hawtin at oracle.com> wrote:
>> [These are my own opinions.]
> `Dog dog = [blah, blah];`? Such code is not a good way to communicate
> ideas between programmers.
Assuming Dog is a standard concrete class
Dog dog = {blah, blah};
means
Dog dog = new Dog(blah, blah);
If Dog is abstract *and* has a well known standard general
implementation, it'll be one of those. I don't think the latter adds
anything useful.
Any reasonable "Dog" is not a type appropriate to be considered a
collection. Even so, assuming we have `Dog(Blah... blahs)` and
`Dog[Blah... blahs]`, then
Dog dog = [blah, blah];
means
Dog dog = new Dog(blah, blah);
(Only without the ambiguity if the constructors clash.)
> Using words to express meanings is better than using symbols - if it
> doesn't become too verbose. List.of(x,y,z) means a list of x, y, z,
> simple and clear. The wordiness of Java programs makes them easier to
> understand.
It's wordiness that is the problem. "of" does not bring any extra
meaning. We know it's a List because of the target type. You didn't go
for `List.<Thing>of(x, y, z)`, where Thing is perhaps less locally
obvious than List.
> I think we should almost *never* declare a weaker type on
> non-publicized variables, like
> List<Thing> things = new ArrayList<>();
> instead, we should almost always declare the most specific type, like
> ArrayList<Thing> things = new ArrayList<>();
There's nothing here that would stop you carrying on doing that with the
less duplicative syntax.
ArrayList<Thing> things = new(); // or {};
( I don't think that's an unreasonable opinion for java.util
collections. The interfaces are wooly and implementations pick and
choose which methods work (there's one 1.2 collection that has just the
one working mutation method). Even then, many of the bundled collection
implementations do not conform to the "interfaces" they "implement".
However, since before 1.0 the Java way, to a greater or lesser extent,
has been to declare an interface and assign the obvious implementation
to it (usually with more appropriate types). )
Tom
More information about the lambda-dev
mailing list