https://kotlinlang.org logo
#announcements
Title
# announcements
m

mbonnin

10/30/2020, 1:56 PM
Can I assume a
mutableMapOf()
to preserve iteration order on key re-insertion like a LinkedHashMap ?
r

Ryan

10/30/2020, 2:04 PM
no you can’t
👍 1
s

spand

10/30/2020, 2:05 PM
Would be nice of them to provide that guarantee though
m

mbonnin

10/30/2020, 2:05 PM
That answers it, thanks
It's very tempting to assume it
r

Ryan

10/30/2020, 2:06 PM
linkedMapOf()
3
boom
m

mbonnin

10/30/2020, 2:07 PM
On the JVM it's all the same
mutableMapOf()
calls
LinkedHashMap()
which is why it's very tempting to assume the same API as
LinkedHashMap
But maybe on other platforms it's different?
s

spand

10/30/2020, 2:09 PM
LinkedHashMap
is a kotlin type
m

mbonnin

10/30/2020, 2:09 PM
Doesn't it alias on the JVM?
mmm, right no it doesn't
s

spand

10/30/2020, 2:10 PM
Pretty sure it does
m

mbonnin

10/30/2020, 2:10 PM
ok yes, it makes sense then 👍
s

spand

10/30/2020, 2:10 PM
There are no docs though so its hard to say but seems pretty safe assumption that it will be identical to javas on all platforms
Maybe a question for #stdlib
👍 1
n

Nir

10/30/2020, 2:13 PM
I feel like, even if the assumption is "safe" it's still better to use the more specific function, and even type
it documents intent, and also, it may end up protecting your code (depending on how things are written, or modified in the future)
☝️ 2
m

mbonnin

10/30/2020, 2:15 PM
The question whether
LinkedHashMap
keeps order on re-insertion remains
r

Ryan

10/30/2020, 2:17 PM
Why does the order matter for a map?
s

spand

10/30/2020, 2:17 PM
A workaround could be to assume so and make a test that verifies in order to guard against it giving you a heads up
n

Nir

10/30/2020, 2:17 PM
Sorry can you clarify "re-insertion"
are you saying, if you do
x["foo"] = 5
if "foo" is already present in the hash table, then you want the value to be changed, but not the order?
m

mbonnin

10/30/2020, 2:18 PM
@Nir yes, exactly
The JVM LinkedHashMap has a specific note about it but it's not mentioned in the Kotlin doc
n

Nir

10/30/2020, 2:22 PM
yeah, guess you'd need to ask the stdlib maintainers to clarify that point
you're interested in all 3 targets?
m

mbonnin

10/30/2020, 2:23 PM
TBH, the code only runs on the JVM at the moment but I like to write something that's as platform agnostic as possible as it might be compiled to native at some point
s

spand

10/30/2020, 2:24 PM
2
m

mbonnin

10/30/2020, 2:24 PM
Thanks !
n

Nir

10/30/2020, 2:28 PM
A good workaround for now might be to create an alias, and use that. If it turns out that the native type does not give the guarantees you need, you should be able to change the alias (hopefully conditionally if necessary)
I'd probably also clarify what's meant by "assume order of re-inserted keys", because allowing users to assume that the order is that re-inserted keys get moved to the front is also a valid choice
g

gildor

10/30/2020, 2:46 PM
no you can't
This is incorrect! Please check documentation of this function: The returned map preserves the entry iteration order.
m

mbonnin

10/30/2020, 2:47 PM
The "key" (pun intended 🙃) word up there is "re-insertion"
For now I'll keep iterate
Lists
I think, it's a bit more verbose/less optimal I think but at least it'll make it explicit that the order is kept
n

Nir

10/30/2020, 2:58 PM
@mbonnin that's a bit tricky to get right
in the face of deletions