dimsuz
12/09/2021, 12:00 PMfun <R> task(body: () -> R): R = TODO()
fun <P1, R> task(body: (P1) -> R): R = TODO()
fun main() {
// overload ambiguity
task {
33
}
}
But no ambiguity for these two:
fun <P1, R> task(body: (P1) -> R): R = TODO()
fun <P1, P2, R> task(body: (P1, P2) -> R): R = TODO()
fun main() {
task { v: Int -> 33 }
}
Fleshgrinder
12/09/2021, 12:17 PMtask { v: Int -> 33 }
The problem with the …
task { 33 }
… is that the compiler cannot choose between () -> R
and (Int) -> R
because both could match:
task { 33 }
task { it.toInt() } // implicit argument
dimsuz
12/09/2021, 12:21 PMtask<Unit> { 33 }
while having other 1 or more arity overloads without this explicit type
task { v: Int -> printLn(v) }
OR
I'd have to do something like
task0(body: () -> Unit)
task1(body: (P) -> Unit)
etc. Both of which I don't like very much 🙂
I wish compiler could be instructed somehow to not expect implicit argument...Fleshgrinder
12/09/2021, 12:27 PMtask { -> 33 }
… as this provides the missing information to the compiler.
I don't know of a way on how to help the compiler here other than on the calling side. 🤔dimsuz
12/09/2021, 12:28 PMFleshgrinder
12/09/2021, 12:30 PM||
or |it|
and the anon fun cannot go without it. It's cool that Kotlin opted for reduced syntax requirements as it keeps our code less noisy, but it has these weird edge cases that are surprising if you don't know what's behind it.dimsuz
12/09/2021, 12:31 PMFleshgrinder
12/09/2021, 12:36 PMI wish compiler could be instructed somehow to not expect implicit argument...This could be achieved with an annotation. I think you have a good case for a feature request here, or maybe it even already exist. I'd say this is worth a search in the issue tracker. 🙂
dimsuz
12/09/2021, 12:43 PMFleshgrinder
12/09/2021, 12:46 PM