Can someone explain the value of collection litera...
# random
t
Can someone explain the value of collection literals? I sincerely don't understand why listOf(), mapOf(), Pairs, and other existing utilities don't do the job effectively enough for some folks.
👍 4
s
thomasnield: It can be very useful for nested collections or multidimension array. Visual difference like the difference between XML and JSON.
n
As one person commented in the Kotlin blog post (https://blog.jetbrains.com/kotlin/2017/06/kotlin-future-features-survey-results/), collection literals would be too difficult to implement (large number of use cases to cover), and the feature makes Kotlin more difficult to learn with no significant benefits. Many Kotlin users would agree on avoiding adding implicit features to Kotlin itself.
v
@speshuric Then my questions is why you you need multidimensional arrays and how often you need them. Because in my experience programming ML, matrixes are always too big to be written by hand (100s of rows)
👍 3
l
and for e.g. json a nice little typesafe builder can be much more readable than list of lists
👍 3
j
Maybe if get a
literal
keyword in the same way we have an
operator
keyword, then users can define their own implementations for
[]
and
{}
, but then still keep it out of the language. Just like with operators, only define a fixed list of literals that will be supported, but don’t actually bring it into the language, if you want to use
[]
litereals, you need to write your own implementation for it or use third-party libs for it.
Something like:
literal fun  String.squareBrackets(varargs String val)
v
p
after careful consideration I am now against adding collection literals to the language.. you guys have turned me over 🙂
simple smile 1
1
t
And what about DSL? Is that not a better and clearer approach to declaring complex data structures?
e.g. Groovy like builders
p
I know I would love a sort of
literal
keyword so you can define your own version, you also could use Array/List liberals in table based testing for example. I also thought that it was an odd nuisance of Kotlin t hat there wasn’t any special syntax for collection literals.
👍 1
j
I think a
literal
keyword solves the problem nicely, it allows devs to use collection literals without the Kotlin devs having to worry which implementation to choose for the literal.
p
It would also make for some great DSLs
j
Indeed, I can then define HTML syntax simply by writing pure JSON
Copy code
html : {
   header: {
       title: "TESTING"
    },
    body: {
        
    }
}
p
BRING BACK THE GROOVY BUILDERS
d
Copy code
html {
   header {
       title("TESTING")
    }
    body {
        
    }
}
it can be like this in kotlin at current state
j
that’s not too bad either
p
I assume that’s just a bunch of functions?
j