Hello! My question was asked in getting-started ch...
# compiler
p
Hello! My question was asked in getting-started channel. But I think it is more relevant here. Maybe there is an issue in compiler. Consider code below:
Copy code
interface A {
    fun x() : Int
}

interface B : A {
    override fun x(): Int = 1
}

open class C: A {
    final override fun x(): Int = 2
}

class D: C(), B // Error : Class 'D' must override public final fun x(): Int defined in C because it inherits many implementations of it
Why D not prefer C::x over B::x? B::x is the interface's default function that must be applied only if there is no implementation of that method in class (even class having implementation is super). I checked such case in Java - there is no such issue there, Java compiler is choosing C::x.
d
This is a designed behavior
Why
D
not prefer
C::x
over
B::x
?
I can ask why not opposite? In Kotlin the semantic difference between classes and interfaces is quite minimal, so it's not fair to choose between those functions only based on class kinds of
B
and
C
t
From the Kotlin specification: • If a derived classifier type inherits several matching concrete declarations from its supertypes, it is a compile-time error (this means a derived classifier type should override such declarations).
🙏 1
p
Dmitriy, thanks for your answer! But what if function is final? How to tell compiler what to choose without actual overriding?
d
You can't Redesign your hierarchy or use composition instead of inheritance
🙏 1