list literal gotcha and suggestion

Gene Ray evildeathmath at
Thu Oct 1 07:40:56 PDT 2009

--- On Thu, 10/1/09, Reinier Zwitserloot <reinier at> wrote:

> From: Reinier Zwitserloot <reinier at>
> Subject: Re: list literal gotcha and suggestion
> To: "Gene Ray" <evildeathmath at>
> Cc: coin-dev at
> Date: Thursday, October 1, 2009, 1:25 PM
> How is a mere constant factor of 3
> relevant when the set/list contains so few entries? Writing
> code with performance in mind before you actually run into a
> performance problem is bad, but designing the language with
> this in mind is undoubtedly worse.

Given that the performance characteristics of the data structures are well-understood and have been for decades, I fail to see how taking them into account when designing a language feature is premature.  As an analogy, if you were in the position several years ago of writing Collections.sort()/Arrays.sort() for the first time, would you suggest using an O(n^2) sort algorithm such as bubble sort instead of an O(n log n) merge-sort or quicksort when writing a new library simply because the as-yet-unwritten library hadn't yet experienced performance issues?


> For example, instead of stubbornly denying that
> Set literals aren't as important as List literals, can
> you, as an avid set user, shed some light on how acceptable
> some of the alternative proposals are, such as:
> [1, 2, 3].toSet();
> new Set[1, 2, 3];	//keep in mind that without
> the 'new', this syntax is pretty much impossible to
> integrate into the existing java grammar, due to looking
> like an array dereference operation.
> new HashSet<>([1, 2, 3]);

Considering that the main point of introducting collection literals of any kind would be to reduce perceived boilerplate, I see minimal value in implementing Set literals in a fashion that is by definition tacking additional boilerplate onto the List syntax.

> NB: I tested on a core2duo mac, version
> 1.6.0_15, and I used sets/lists of Integer instead of
> String.

Which implementation of set/list did you use?  w/ArrayList and HashSet of Integer, I find the point of equal-performance to be somewhere between 2 and 3 elements on a Core2Duo WinXP machine.

> NB2: I whole heartedly agree with Stephen
> Colebourne's sentiment that I don't believe adding
> set literals is somehow going to make java joe see the light
> and start using set a lot more.

I unfortunately suspect that this assessment is correct.  I disagree that it is a reason to cater solely to the hypothetical "Java Joe" point of view when attempting to evolve the language.  Currently, with Set and List both being aspects of a core library with equivalent syntax, they are on equal footing; introducing a new inconsistency to the basic syntax where none now exists without a strongly compelling reason to do so is dubious, and adding one very specific change while deliberately leaving other parallel changes out in order to appeal to what the broadest-base of programmers might find convenient now is (to blatant purloin a phrase from one of Neal Gafter's posts) "a lack of consideration for the long-term evolution of the language."


More information about the coin-dev mailing list