list literal gotcha and suggestion

Mark Thornton mthornton at optrak.co.uk
Thu Oct 1 12:05:14 PDT 2009


Reinier Zwitserloot wrote:
> 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]);
>
>   
There is another alternative: The value [1,2,3] should be both a List 
and a Set while [1, 2, 1] would be just a List. As the elements are 
compile time constants the compiler can easily check for duplicates. If 
any element appears more than once, then the result is just a List, 
otherwise it implements both List and Set.

Regards,
Mark Thornton




More information about the coin-dev mailing list