Hey guys! Joined to ask a simple question about on...
# random
v
Hey guys! Joined to ask a simple question about one of the features. Here: https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-overload-resolution-by-lambda-return-type/ the documentation states that
This annotation is also used to discriminate the annotated overloads in case if overload selection still cannot choose one of them even taking in account the result of lambda parameter analysis. In that case a warning is reported.
But I have read this like 10 times, and I don't get which function do I annotate, how exactly is Kotlin going to discriminate? Do I annotate the preferred function or the other? Running the example code in Kotlin playground doesn't make any difference whatsoever no matter which one I annotate
k
Have you tried leaving out the annotation altogether? Also what do you mean by "preferred function"? The argument you pass is going to be either a
() -> Int
or a
() -> Double
, it can't be both so there's no room for "preference". Thus, I would annotate both functions (for the sake of symmetry) even though you only have to annotate one of them.
v
Well my use case is that one of the overloads can be considered a parent type of the other. E.g. one is
Result<T>
and the other is just
T
For example:
fun <T> chain(block: () -> T)
fun <T> chain(block: () -> Result<T>)
And I of course want to make the more specific function be chosen first
Well turns out that the compiler will never choose the more generic function no matter which or how many functions you annotate. Took me a botched release of kmmutils to figure this out