https://kotlinlang.org logo
#kotlin-native
Title
# kotlin-native
k

Krystian

12/29/2022, 11:22 PM
I see that K/N converts nested structs themselves into Vals (immutable) for example here:
Copy code
@kotlinx.cinterop.internal.CStruct public final class Camera2D public constructor(rawPtr: kotlinx.cinterop.NativePtr /* = kotlin.native.internal.NativePtr */) : kotlinx.cinterop.CStructVar {
    @kotlinx.cinterop.internal.CStruct.VarType @kotlin.Deprecated public companion object : kotlinx.cinterop.CStructVar.Type {
    }

    public final val offset: kaylib.Vector2 /* compiled code */

    public final var rotation: kotlin.Float /* compiled code */

    public final val target: kaylib.Vector2 /* compiled code */

    public final var zoom: kotlin.Float /* compiled code */
}
offset and target are both vals and I can't simply modify them once alloc'ed, but I can modify their values which are .x and .y which isn't that much of a problem, but it does cause a little bit of dull modifications of .x and .y one by one (and I'm very sure its for a good reason) There is no other way to mitigate this at all by somehow being able to mutate offset or target as one, rather than mutating its members?
l

Landry Norris

12/30/2022, 2:11 PM
I’ve found that when cinterop creates a val, that usually means that the struct didn’t use a pointer to, in your case, Vector2. If the struct declaration didn’t use a pointer, the memory layout of the Vector2 gets embedded in the memory layout of your Camera2D, meaning you can’t ‘set’ the location. Even in C, when you set
camera2D->target = something else
, it’s a shortcut for setting x and y.
You could create an extension
Vector2.set(other: Vector2)
k

Krystian

12/30/2022, 3:04 PM
Thanks, I will give that a try and see if that will make it work
Is there any rational “reason” behind this anyway? Why set it to val if it doesn’t use a pointer?
Just would love to learn more about K/N and it’s reasons
l

Landry Norris

12/30/2022, 3:12 PM
Kotlin uses reference types.
k

Krystian

12/30/2022, 3:22 PM
Yep using extension made it easier, thank you. This is going to be useful
l

Landry Norris

12/30/2022, 3:23 PM
I’m not 100% sure, but I think there’s a way to set up an operator function so you can use ‘=’ instead of .set
k

Krystian

12/30/2022, 3:32 PM
Hmmm I don’t think there is a way to overload the = operator
l

Landry Norris

12/30/2022, 3:34 PM
Yeah, I don’t see an easy way to create a delegate for the properties, so setValue won’t work.
k

Krystian

12/30/2022, 3:46 PM
Oh well, this works good enough as it is!
13 Views