Hi all, I wanted to provide some new samples for t...
# stdlib
l
Hi all, I wanted to provide some new samples for the stdlib docs but I was a bit surprised about the hashMapOf example.
Copy code
val map = hashMapOf<Int, Any?>()
println("map.isEmpty() is ${map.isEmpty()}") // true

map[1] = "x"
map[2] = 1.05
// Now map contains something:
println(map) // {1=x, 2=1.05}
From my understanding the last assert is not general enough as the order of elements is not guaranteed, right? This could maybe be changed to two asserts checking for the two elements separately
e
insertion order is preserved for
mapOf()
,
mutableMapOf()
, and various collection extensions returning
Map<>
, but yeah it isn't guaranteed for
HashMap
but the print isn't an assertion so IMO it's fine here
l
This is just the human readable translation of the unit test which has an actual assert:
Copy code
assertPrints(map, "{1=x, 2=1.05}")
So a change in the implementation might break the unit test and therefore fail the build
d
If order is important, don’t use the hash map that doesn’t guarantee order. Otherwise, why not assert that two maps contain the same entries (ordering notwithstanding)?