Is downcasting supported via reverse C interop? F...
# kotlin-native
a
Is downcasting supported via reverse C interop? For example, given the following Kotlin definitions,
Copy code
interface Interface {
    fun foo(): Int
}

class Concrete : Interface {
    override fun foo() = 42
    fun bar() = 1337
}

fun getInterface(): Interface = Concrete()
is it legal to check
IsInstance
and "cast" by using the pinned interface pointer as the concrete type?
Copy code
KotlinInterfaceTest_ExportedSymbols* lib = KotlinInterfaceTest_symbols();
auto sample = lib->kotlin.root.sample;

KotlinInterfaceTest_kref_sample_Interface interface = sample.getInterface();
if (lib->IsInstance(interface.pinned, sample.Concrete._type()))
{
    auto concrete = KotlinInterfaceTest_kref_sample_Concrete{ interface.pinned };
    printf("Call concrete method: %d\n", sample.Concrete.bar(concrete));
}
This seems to work (prints "Call concrete method: 1337"), but want to make sure I'm not relying on undefined behavior. Thanks!
o
yes, this is intended casting strategy
👍 1
a
Great, thanks!