It is a way of not needing the Pair for a certain number of name/value pairs - maybe 10 or so name value pairs before needing to use Pair. Perhaps it could extend with vararg but it seems to me you would lose type checking?
fun<K,V> mapOf(key1:K, value1:V) : Map<K,V> {
return linkedMapOf(Pair(key1,value1))
}
fun<K,V> mapOf(key1:K, value1:V, key2:K, value2:V) : Map<K,V> {
return linkedMapOf(Pair(key1,value1), Pair(key2,value2))
}
fun<K,V> mapOf(key1:K, value1:V, key2:K, value2:V, key3:K, value3:V) : Map<K,V> {
return linkedMapOf(Pair(key1, value1), Pair(key2, value2), Pair(key3, value3))
}
fun<K,V> mapOf(key1:K, value1:V, key2:K, value2:V, key3:K, value3:V, key4:K, value4:V) : Map<K,V> {
return linkedMapOf(Pair(key1, value1), Pair(key2, value2), Pair(key3, value3), Pair(key4, value4))
}
… maybe support up to 10 key/value pairs?
// example 1: … removing need for Pair
val m1a = mapOf( “one”, 345, “two”, 123, “three”, 45) // this
val m1b = mapOf( Pair(“one”, 345), Pair(“two”, 123), Pair(“three”, 45)) // rather than this
// example 2:
val m2a = mapOf( “one”, Country(“NZ”,“New Zealand”), “two”, Country(“AU”,“Australia”)) // this
val m2b = mapOf( Pair(“one”, Country(“NZ”,“New Zealand”)), Pair(“two”, Country(“AU”,“Australia”))) // rather than this
Yes, it is possibly a bit of a hack purely for aesthetic value. I have seen the approach in java land.
Cheers, Rob.