mzgreen
01/19/2023, 7:45 AMcoerceAtLeast 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 placeTimo Gruen
01/19/2023, 7:55 AMTimo Gruen
01/19/2023, 7:56 AMfun <A, B : Foo<A>> B.bar() { }
would be the same to your
fun <A> Foo<A>.bar() { }Timo Gruen
01/19/2023, 7:57 AMmzgreen
01/19/2023, 7:57 AMmzgreen
01/19/2023, 7:58 AMCLOVIS
01/19/2023, 8:47 AMInt  doesn't implement your Foo interface, so it's never possible for it to be Foo of itself.mzgreen
01/19/2023, 8:51 AMmzgreen
01/19/2023, 8:52 AMclass Test: Foo<Test> {
    override fun foo() {}
}
fun main() {
    val test = Test()
    test.bar()
}CLOVIS
01/19/2023, 9:00 AMmzgreen
01/19/2023, 9:01 AMCLOVIS
01/19/2023, 9:02 AMmzgreen
01/19/2023, 9:04 AM