I'm trying to write code which maps some Kotlin va...
# kotlin-native
r
I'm trying to write code which maps some Kotlin values into cinteropped ones, and stumbled upon this one trying to process array (for primitives copying values via
output.pointed.value = input
, for structures I was going to copy bytes and needed size of generic
CVariable
). The stuff that breaks:
Copy code
import kotlinx.cinterop.*

inline fun <reified T : CVariable> sizeOf(value: T) = sizeOf<T>()

fun process(value: Any) {
	when (value) {
		is Int -> 4
		is CVariable -> sizeOf(value)
	}
}

fun main() = Unit
The error:
Copy code
error: compilation failed: native variable class deserialized class CVariable must have the companion object

 * Source files: sample.kt
 * Compiler version info: Konan: 0.9.1 / Kotlin: 1.3.0
 * Output kind: PROGRAM

exception: java.lang.IllegalStateException: native variable class deserialized class CVariable must have the companion object
s
This code works exactly as it should, because
CVariable
class doesn’t have any particular size.
Btw, what prevents you from using
CValue<*>
for structs?
r
Oh, I completely missed the fact that
Type
is present, but is not a companion in these abstract classes. Of course it's logical. Regarding
CValue<T>
-- I've forgot about it as I only saw it in function signatures and not entirely understood the idea behind it. Right now I'm preparing
List
of `GValue: CStructVar`s, allocated within
memScoped
block, and in the other place getting them from list and trying to convert the list into
void*
. Should I use
CValue<GValue>
in the first place instead?
s
Should I use
CValue<GValue>
in the first place instead?
Yes, I suppose so. This may seem a bit inconvenient, but
CValue<GValue>
corresponds to the value of the structure (which doesn’t depend on any native memory and remains valid after leaving
memScoped {}
), while
GValue
is much like a pointer. For example,
CValue<IntVar>
is mostly equivalent to
Int
.
r
I see. And in my function similar to
process
above I should be able to check
is CValue<*>
and will have
.size
handy. Cool, thanks, will try!
👍 1