Improved List initialisation
I hope this is the correct mailing list to discuss such an idea, please correct me if I'm wrong, but core-libs sounded like it included collections api. Within Java we have a sugared syntax for initialising arrays, so for example we can write this: int[] intArray = {1,2,3}; However, list initialisation requires a very handed approach, for example: List<Integer> intList = new ArrayList<Integer>(); intList.add(1); intList.add(2); intList.add(3); Certain other languages have some sugar for this kind of thing, the general jist of: List l = [1,2,3]; In java we could get a similarly concise effect, with no hacking in syntactic sugar to solve these problems, I am thinking of a syntax along the lines of: List<Integer> intList = new ArrayList<Integer>(1,2,3); This could be trivially implemented as such: public ArrayList(T ... args) { for (T t : args) { this.add(t); } } The same approach could be used for Vector and the Set collections classes I am also not aware of the process of proposing library changes, eg would I need to do something in conjunction with the JCP? Perhaps someone more knowledgable could advise me. Awaiting commentary with interest. Richard Warburton
On 9/11/07, Richard Warburton <richard.warburton@gmail.com> wrote:
In java we could get a similarly concise effect, with no hacking in syntactic sugar to solve these problems, I am thinking of a syntax along the lines of:
List<Integer> intList = new ArrayList<Integer>(1,2,3);
Most welcome! The one-liner I use is new ArrayList(Arrays.asList(1,2,3)); but it goes on my nerves. On a side note, I would be interested in the history of var-args in this context - in the tiger release we were given var-args and at the same time shunned away from arrays. Was List considered for varargs? Nowadays I code many methods twice: void f(X...x) { f(Arrays.asList(x)); } void f(List<X> x) { ... }
participants (2)
-
Matthias Ernst
-
Richard Warburton