Hello there, Hope you are all having a blast. I a...
# announcements
a
Hello there, Hope you are all having a blast. I am trying to understand multifield value classes so I have dumbed it down and I need some correction If I am wrong 1. Multifield value classes are like Cpp Structs, and normal classes (including data classes) are like Cpp Classes, Correct? 2. Does that translate to stack and heap allocations for Kotlin/Native?
n
Cpp, you mean C++? Class and struct are just virtually identical keywords in C++, any other difference is a convention
n
In C++, there are technically no structs, the
struct
keyword just defines a class where all members are public by default
a
@Marc Knaup That actually confused me at some point
­čśü 1
m
Just consider value classes to be by-value copied in many cases but wrapped as references internally in other cases.
a
@Marc Knaup you really dumbed that down pretty nicely. Thanks
m
As mentioned in that KEEP doc, on Native the compiler is free to implement them however it likes. Therefor it will likely have use stack allocation whenever possible.
n
@Niklas G├╝rtler and default to public inheritance instead of private :-)
It will probably only put them on the stack if they're small, if they're more than 3-4 fields it's probably not worth it
a
@Niklas G├╝rtler @Nir Is it safe to compare them with Swift Structs and Swift Classes?
n
No
Structs in swift are explicitly passed by value
I.e. it always makes a copy
Read Marc's link
a
I see, but in value classes, it might sometimes pass by reference and pass by value. right? No guarantees. Am I getting a hang of this?
m
Swift allows read-only and read-write properties in structs. Kotlin only allows read-only properies in value classes. That is because mutating a single property is actually considered a mutation of the entire struct held in a mutable variable/property.
a
I started by reading that. Thats why I then came here afterwards, I needed just a few clarifications
Okay, now that makes a lot of sense with what I was reading in the article @Marc Knaup \
­čĹŹ 1
n
If something is immutable then you can't actually tell if it's being passed by reference or value.
that's the clever bit