Python like dictionaries in Kotlin


#1

Hi,
I’m newbe in Kotlin thus sorry if I’m asking for someting obvious. Just wondering whether it is possible to achieve something similar to Python dictionaries in Kotlin namely (pseudocode):

dict1 = {“firstName”: “John”, “age”: 31, “married”:true}

in Kotlin so:
Some kind of map of properties of different types (Integer, Boolean, String) mapped by key (String).
Thanks for any hints
JS


#2
val dict1 = mapOf( 
    "firstName" to "John",
    "age" to 31,
    "married" to true
)

You get a Map<String, Any> from that sort of construct.


#3

Do you know why they decided to use ‘to’ instead of : or => or something else known from other languages?


#4

I guess, it’s because symbolic operators are (intentionally) pretty limited in Kotlin.


#5

to is syntactically not an operator but an infix extension method on Any. So you can define your own alternatives to to as long as they are valid identifiers.