top | item 17138098

(no title)

hoodoof | 7 years ago

val list = listOf("Saab", "Volvo")

val map = mapOf("firstName" to "John", "lastName" to "Doe")

The above seems a really strange decision for a "new" language - why would you not just use the most common structure, being JSON?

discuss

order

kllrnohj|7 years ago

Perhaps worth noting that none of that is langauge syntax, it's library syntax. mapOf takes vararg pairs: Pair<K, V>, and then the 'to' operator is an infix function that makes a Pair<K,V>.

So you could trivially do your own listOf or mapOf that creates whatever data structure you want, whereas languages with special syntax for this tend to hardcode the result of it. That's a valid choice, sure, but just that there are tradeoffs here rather than it being obviously better one way or the other.

singularity2001|7 years ago

how can a newly designed language not make lists and maps first class citizens?

DeonPenny|7 years ago

Because it allowed them to use basic understandable functions rather than writing special use cases. listof and mapOf are just functions and to is just a infix function that makes a pair out of two elements. It makes far more sense for someone making a language.

cytzol|7 years ago

JSON doesn’t support non-String keys, for one thing.

bobbyi_settv|7 years ago

One of my biggest annoyances in Scala is not having literal syntax for sequences and maps. The language seemingly tries to have every syntax convenience that's ever existed in every language, even when they conflict with each other and cause confusion, and then misses the most basic one.

I've been told that it's because "how would it know which specific sequence and map class you want", but it already has Seq() and Map() which give you the default immutable sequence and map classes, so obviously a literal syntax would give you the same one.

singularity2001|7 years ago

Evidence that designers of Kotlin have no taste