kevin.cianfarini
12/04/2020, 7:26 PMDominaezzz
12/04/2020, 8:32 PMkevin.cianfarini
12/04/2020, 8:34 PMDominaezzz
12/04/2020, 8:36 PMkevin.cianfarini
12/04/2020, 8:36 PMDominaezzz
12/04/2020, 8:37 PMkevin.cianfarini
12/04/2020, 8:37 PMreified
is a thing...but that could be a no-op in nativeDominaezzz
12/04/2020, 8:38 PMkevin.cianfarini
12/04/2020, 8:38 PMYeah multiplatform would be incredibly difficult if they didn't align.Do you have any examples that highlight this?
Dominaezzz
12/04/2020, 8:38 PMkevin.cianfarini
12/04/2020, 8:40 PMDominaezzz
12/04/2020, 8:40 PMkevin.cianfarini
12/04/2020, 8:40 PMDominaezzz
12/04/2020, 8:41 PMkevin.cianfarini
12/04/2020, 8:41 PMDominaezzz
12/04/2020, 8:41 PMkevin.cianfarini
12/04/2020, 8:44 PMA
interface A { fun foo() }
and it has two implementations, B
and C
class B : A { override fun foo() = Unit }
class C : A { override fun foo() = println("") }
When making a call to an instance of B or C, the JVM will look into the Virtual method table to figure out which function to be invoked. I'm not quite sure why this is necessary, as this seems like it could be totally enforced by the compiler?
I have very limited compiler knowledge, so please bare with meDominaezzz
12/04/2020, 8:45 PMkevin.cianfarini
12/04/2020, 8:46 PMDominaezzz
12/04/2020, 8:47 PMkevin.cianfarini
12/04/2020, 8:47 PMval bar = listOf(C(), B())
bar.forEach { it.foo() }
Dominaezzz
12/04/2020, 8:48 PMkevin.cianfarini
12/04/2020, 8:48 PMDominaezzz
12/04/2020, 8:48 PMkevin.cianfarini
12/04/2020, 8:48 PMDominaezzz
12/04/2020, 8:49 PMkevin.cianfarini
12/04/2020, 8:49 PMDominaezzz
12/04/2020, 8:50 PMkevin.cianfarini
12/04/2020, 9:10 PMkotlin-native
on stackoverflowinterface A
class B : A
class C : A
fun foo(a: A)
If the same method that rust employs were to be taken in Kotlin, then polymorphism is really shorthand for generic bounds. It could be transformed into the following
fun <T : A> foo(a: T)
Using monomorphism, the compiler would search through all usages of this function to see what concrete types actually call this function. And then it would implement them for those classes.
fun foo_b(a: B)
fun too_c(a: C)
Could this method be employed for class inheritance and not interfaces? I don't know. Rust doesn't allow inheritance and this might be the reason they can pull off total static dispatch.
At the very least, it seems like Kotlin might be able to do it for interfaces.Dominaezzz
12/05/2020, 8:37 PM