This syntax is already in the language if you a wi...
# language-proposals
v
This syntax is already in the language if you a willing to prefix your "literal" with a keyword like
arr["a", "b", "c"]
(can be implemented via
get
http://kotlinlang.org/docs/reference/operator-overloading.html#indexed)
e
voddan: oh, really pleased to see there are now even more operators
[i, ... i_n]
didn't exist before
v
@elect When did it not exist?
operator get
is there from very early versions. This is how
[]
were always implemented
e
there was only one argument get and set
­čĺę 1
I guess
g
Get and set operators not changed
d
This approach has a hidden performance problem: 2 allocations instead of 1, one for vararg array, and another for the resulting collection. In fact, you can already do "collection builders", which do not suffer from additional allocations and are in fact more flexible. Syntax might be a bit more verbose compared to what you'd expect from collection literals / collection comprehensions.
v
@dmitry.petrov can't we reuse the vararg array unto the list?
d
Yes, sure. You'll need a (Mutable)List implementation that will expose the corresponding constructor. Also possible (but probably somewhat less effective) for sets and maps. Yet, I'd prefer builder-based approach myself.