Hey folks, Kotlin has a
coerceAtLeast
function which is defined as:
fun <T : Comparable<T>> T.coerceAtLeast(minimumValue: T): T
So any type that is comparable can invoke this function, for example this works:
"foo".coerceAtLeast(...)
.
I’ve created a similar function:
fun interface Foo<in T> {
fun foo()
}
fun <T : Foo<T>> T.bar() {}
but when I call it like this:
val test = Foo<Int> {}
test.bar()
it doesn’t work. It fails to compile with error:
Unresolved reference. None of the following candidates is applicable because of receiver type mismatch: public fun <T : Foo<TypeVariable(T)>> TypeVariable(T).bar(): Unit defined in root package in file File.kt
why is that? I don’t see any difference between this and
coerceAtLeast
. I can define more extensions on
Comparable
type and they work fine but my custom type doesn’t.
It works only if I change:
fun <T : Foo<T>> T.bar() {}
to:
fun <T> Foo<T>.bar() {}
but these don’t have the exact same meaning and I still don’t get why the first one doesn’t work in the first place