list literal gotcha and suggestion
Nick Parlante
nick.parlante at cs.stanford.edu
Tue Sep 29 11:20:03 PDT 2009
(snip lots of discussion)
The fact that list-[] syntax is so widely used I find to be a pretty
convincing argument, so I can live with list-[]. Even the Java
Collections toString() and Arrays.toString() use list-[] to print.
But, if list-[] is the syntax, then I think the {} syntax should not
represent a set, so the obvious misuse is a compile error. My gotcha
example new ArrayList<Integer>({3, 1, 4, 1}) looks reasonable but does
something else -- I've found that to be exactly the time and enthusiasm
destroying combination for students. If the set literal were some
awesome feature the tradeoff might be worthwhile, but set literals seem
kind of rare compared to lists and maps. Indeed, list construction is
probably the single most common case, so making the common case a little
more error-prone for the benefit of the rare case seems like a bad tradeoff.
As a supporting example and as Reinier pointed out, Python does
fine without a set literal, so in Java that would be
new HashSet<>([1, 2, 3])
which doesn't seem like a hardship.
Cheers,
Nick
More information about the coin-dev
mailing list