https://kotlinlang.org logo
Title
k

Karlo Lozovina

10/12/2021, 8:46 PM
is there a way to choose what concrete type
associateWith
returns?
🇳🇴 1
h

hho

10/12/2021, 9:03 PM
No.
LinkedHashMap
is hardcoded in the stdlib. You could only use
associateWithTo
to add the results to an existing (mutable) map of your choice.
👍 2
k

Karlo Lozovina

10/12/2021, 9:17 PM
thanks!
e

ephemient

10/12/2021, 9:29 PM
Kotlin specifies that
mapOf
,
mutableMapOf
, and most Map-returning methods preserve insertion order, so in practice they are backed by LinkedHashMap
👍 1
but yes,
associateWithTo
(like other
*To
methods) returns the collection itself, so you can write
val myMap = foo.associateWithTo(MyMapType()) { ... }
with the result of
myMap: MyMapType
👍 1