#KT-537 adds A.to(B) helper method to make easy creation of map literals: hashMap("a" to 3, "b" to 6) etc
This commit is contained in:
@@ -73,6 +73,15 @@ public inline fun <T> Iterator<T>.toSortedSet() : SortedSet<T> = toCollection(Tr
|
||||
*/
|
||||
public inline fun <T> Iterator<T>.toSortedSet(comparator: Comparator<T>) : SortedSet<T> = toCollection(TreeSet<T>(comparator))
|
||||
|
||||
|
||||
/**
|
||||
* Creates a tuple of type [[#(A,B)]] from this and *that* which can be useful for creating [[Map]] literals
|
||||
* with less noise, for example
|
||||
|
||||
* @includeFunctionBody ../../test/MapTest.kt createUsingTo
|
||||
*/
|
||||
public inline fun <A,B> A.to(that: B): #(A, B) = #(this, that)
|
||||
|
||||
/**
|
||||
Run function f
|
||||
*/
|
||||
|
||||
@@ -119,6 +119,13 @@ class MapTest {
|
||||
assertEquals(2, map.get("b"))
|
||||
}
|
||||
|
||||
test fun createUsingTo() {
|
||||
val map = hashMap("a" to 1, "b" to 2)
|
||||
assertEquals(2, map.size)
|
||||
assertEquals(1, map.get("a"))
|
||||
assertEquals(2, map.get("b"))
|
||||
}
|
||||
|
||||
test fun createLinkedMap() {
|
||||
val map = linkedMap(#("c", 3), #("b", 2), #("a", 1))
|
||||
assertEquals(1, map.get("a"))
|
||||
|
||||
Reference in New Issue
Block a user