list literal gotcha and suggestion

Schulz, Stefan schulz at e-spirit.de
Wed Sep 30 06:21:06 PDT 2009


Rémi wrote
> > Just wanted to add a little side-note that, despite not 
> being technically confusable with Lists, Map literals would 
> behave exactly like Sets wrt. duplicate keys. I'd rather 
> prefer clearly typed factory methods over potentially 
> confusing literals in the language. The only really awkward 
> thing regarding creation to me are Maps.
> >    
> 
> Hi Stefan,
> I don't follow you. With a literal syntax, the compiler can detect 
> duplicate keys at compile time
> if keys are constants.

Sorry, if my comment was too unclear. As the discussion included, whether to use [] or {} for Sets vs. Lists, I just wanted to add that, visually, with the suggestion by Stephen one still has curly braces for Lists, which keep duplicate entries, as well as for Maps, which remove duplicates similar to Sets. As said, this is no technical issue, but may be confusing especially wrt. not having a Set literal.
Thinking of it, I am not sure, if this would make people abuse Map literals for Sets. Would the following be valid Java?
Set<String> set = {"a" :0, "b" :0, "c" :0}.keySet();

Stefan



More information about the coin-dev mailing list