Reputation: 7472
For example I have following sample code
fun f<T>( cb: (T, Int) -> Unit ): Unit {
println("f called with cb which accepts 2 arguments");
}
fun f<T>( cb: (T) -> Unit ): Unit {
println("f called with cb which accepts 1 argument");
f<T> {item, position -> cb(item) }
}
fun main(args : Array<String>) {
f { item -> }
f { item, position -> }
}
Obliviously I want f
function choose right implementation depends on amount of arguments I'm going to pass to closure
Currently kompiller gives me error:
(8, 7) : Overload resolution ambiguity:
internal fun <T> f(cb: (T, kotlin.Int) -> kotlin.Unit): kotlin.Unit defined in root package
internal fun <T> f(cb: (T) -> kotlin.Unit): kotlin.Unit defined in root package
Code in online sandbox: http://kotlin-demo.jetbrains.com/?publicLink=100745728838428857114-628607155
Compiller version: org.jetbrains.kotlin:kotlin-gradle-plugin:0.10.770
UPD: related issue on youtrack: https://youtrack.jetbrains.com/issue/KT-6939
Upvotes: 3
Views: 2106
Reputation: 7472
Thanks to @miensol I realized it was my mistake. I forgot to specify type for T while calling f()
.
Fixed code:
fun f<T>( cb: (T, Int) -> Unit ): Unit {
println("f called with cb which accepts 2 arguments");
}
fun f<T>( cb: (T) -> Unit ): Unit {
println("f called with cb which accepts 1 argument");
f<T> {item, position -> cb(item) }
}
fun main(args : Array<String>) {
f<String> { item -> }
f<Boolean> { item, position -> }
}
Upvotes: 4