Most probably wrong channel (point me to the right...
# stdlib
d
Most probably wrong channel (point me to the right one). I wonder why does this give me an "overload ambiguity" error?
Copy code
fun <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:
Copy code
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 }
}
f
The first one wouldn't give you one if you'd have:
Copy code
task { v: Int -> 33 }
The problem with the …
Copy code
task { 33 }
… is that the compiler cannot choose between
() -> R
and
(Int) -> R
because both could match:
Copy code
task { 33 }
task { it.toInt() } // implicit argument
d
Ah. I see. This breaks my nice api structure. now the client has to either do
Copy code
task<Unit> { 33 }
while having other 1 or more arity overloads without this explicit type
Copy code
task { v: Int -> printLn(v) }
OR I'd have to do something like
Copy code
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...
f
The simplest way for a caller to fix this without super ugly syntax is …
Copy code
task { -> 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. 🤔
👌 1
d
oh, I didn't thought of this. thanks!
f
Note that this is a reason why many languages always require the arguments, in Rust one always has to have
||
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.
d
agreed, most of the time this is nice, but there are edge cases indeed)
f
I 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. 🙂
d
yep, will do! I have already google some posts on reddit which asked similar questions, but answers were less clearer than yours. thanks for the help!
f
😊