```fun <K, V> MutableMap<Set<K>, V&...
# announcements
l
Copy code
fun <K, V> MutableMap<Set<K>, V>.put2(vararg keys: K, value: V) = put(keys.toSet(), value)

fun main() {
    val map = mutableMapOf<Set<Int>, String>()
    map.put2(1, 2, 3, value = "x") // no errors
    map.put2(1, 2, 3, "x") // compiler error: No value passed for parameter 'value'
}
wouldn't it make sense for the compiler to figure the last argument on its own in this scenario?
c
putting values after varargs is confusing to read, there’s nothing at the call-site to suggest the first 3 parameters are varargs. Maybe try moving the
value
into a lambda parameter?
Copy code
inline fun <K, V> MutableMap<Set<K>, V>.put2(vararg keys: K, value: ()->V) = put(keys.toSet(), value())
fun main() {
    val map = mutableMapOf<Set<Int>, String>()
    map.put2(1, 2, 3) { "x" }
}
🤔 1
s
If you change the type of
map
to Map<Set<Any>, String>> do you still expect “x” to be value? Or should it be part of
keys
then?
l
I guess it might end up being confusing in some cases.. @StavFX In case of ambiguity I'd expect a compiler error
s
I’m biased because in Java varargs have to be the last parameters in a method signature, so it doesn’t even make sense to me to put it as the first. But I think the only reason it’s allowed in Kotlin is because you of name parameters. I guess I see your point, the compiler COULD be smart enough to infer it in some cases, but like @Casey Brooks said, I think it’s confusing to write that code even if it worked.