Anyone having issues returning a native var (e.g. ...
# kotlin-native
h
Anyone having issues returning a native var (e.g. ULongVar) from a suspending function? Im getting a typecast exception from NativePointed to the return type (ULongVar in this instance). Reproducer inside ⬇️
Copy code
@Test
    fun suspendReturnNativeVar() {
        suspend fun NativePlacement.returnStruct(): ULongVar {
            val buffer = allocArray<UByteVar>(sizeOf<ULongVar>())

            // Any suspending data fetch function
            delay(1)

            val reinterpret = buffer.reinterpret<ULongVar>()
            reinterpret[0] = 0xAAAAu

            return reinterpret.pointed
        }

        runBlocking {
            memScoped {
                val v = returnStruct()

                assertEquals(0xAAAAu, v.value)
            }
        }
    }
Im getting a
kotlin.ClassCastException: kotlinx.cinterop.NativePointed cannot be cast to kotlinx.cinterop.ULongVarOf
when the coroutine returns to the calling context (when coroutine dispatches the assignment of
val v
)
Removing the suspend keyword (and the delay) makes this work
Tested on 1.4.35 and 1.5.0 with multithreaded coroutines (though no threading involved here)