Given that array literals have already been introduced for annotations (in 1.2 M1), for consistency I’d have thought that arrays should be the default and not lists.
If one of the main reasons for them is to save typing, then to my mind the most sensible plan would be:
-
If the literal contains elements of one of the primitive types, then a dedicated array for that type should be assumed. So [1, 2, 3] would be an IntArray
and [‘a’, ‘b’, ‘c’] would be a CharArray
.
-
In all other cases the generic array type, Array<T>
, should be the default.
As for the other collection types, couldn’t the type of these just be deduced from the context? So, for example:
val ls: List = [1, 2, 3] // List<Int>
val ms: MutableList = ["a", "b", "c"] // MutableList<String>
val l2d: List = [ [1, 2], [3, 4] ] // List<IntArray>
val a2d = [ ['a', 'b'], ['c', 'd'] ] // Array<CharArray>
val aSet: HashSet = [1, 2, 3] // HashSet<Int>
val aList: LinkedList = ["x", foo(), *bar()] // LinkedList<Any?> possibly
val aMap: TreeMap = [1 to "One", 2 to "Two"] // TreeMap<Int, String>
or, if you have a function, say:
fun someFun(h: HashSet<Int>) {
// do something
}
you could call it with a line such as:
someFun([1, 2, 3])
Incidentally, I should mention that initially I was against collection literals but I’ve come around to the idea partly because it now looks inevitable but mainly because I see it as the only way to have a more compact notation for multi-dimensional collections.