https://kotlinlang.org logo
Title
c

christophsturm

03/14/2021, 10:49 PM
it seems i can define 2 methods where the only difference in signature is the number of parameters that their lambda takes:
fun mf(method: suspend () -> String) {
}
fun mf(method: suspend (String) -> String) {
}
and i can call the method that takes one parameter just fine:
mf({string->"string"})
but how can i call the other method?
mf({"string"}) // fails with overload resolution ambiguity
s

Shawn

03/15/2021, 12:46 AM
where the only difference in signature is the number of parameters that their lambda takes
also I mean, sure, disparate arity means different function types, why wouldn’t that be overloadable?
c

christophsturm

03/15/2021, 9:03 AM
thanks, so the solution is
mf({->"string"})
:yes: 1
m

Matteo Mirk

03/24/2021, 10:45 AM
You have to look at the lambda type as a whole. So
() -> String
is a different type than
(String) -> String
, thus overloading is possible
c

christophsturm

03/24/2021, 11:18 AM
sure. I just was not sure how to indicate which one i want to call at the call site.
m

Matteo Mirk

03/24/2021, 11:32 AM
Yeah that was tricky! I learned it from the linked thread as well