list literal gotcha and suggestion
Tomas Salfischberger
t.salfischberger at celerity.nl
Wed Oct 7 13:19:06 PDT 2009
Reinier Zwitserloot wrote:
> The major point you're missing is the proposal to make list literals
> implement a to-be-named concrete implementation of java.util.List that
> has a few useful utility methods, including at least "toSet()". The
> reason being that:
>
> {1, 2, 3}.toSet();
>
> is a lot nicer than:
>
> Collections.unmodifiableSet(HashSet.of({1, 2, 3}));
Good point, I probably skipped over it somewhere.
> There was some discussion about a tiny detail - should we have of()
> methods that take varargs, should we have of() methods that takes a
> Collection (which you could then make with the list literal syntax),
> or should we just stick with the already existing copy constructors?
We would have to think about the possible use-cases we want to support:
- Immutable List literal
- Mutable LinkedList, HashSet etc
- Immutable LinkedList, HashSet etc
Possibly other combinations?
> Then there's another tiny detail: Should these methods be called 'of',
> or 'newList'? If you're *NOT* using static imports, then 'of' is much
> nicer, but if you want to statically import these pseudo-constructors,
> 'of' is useless.
The 'newList' method would still cause static import conflicts with
different List implementations, it would have to be' newLinkedList'
which is just as much characters as 'LinkedList.of'.
Thinking about tool-support I would pick the of() variant, typing
"LinkedL [ctrl+space] .of" would be enough in most current IDEs to get
both the import of LinkedList and the statement. The static import
version would as far as I know require you to manually type the static
import before using the newLinkedList() method.
Tomas
More information about the coin-dev
mailing list