https://kotlinlang.org logo
Title
b

Bence Zsigmond Nagy

01/22/2019, 7:59 PM
How do Kotlin Inline classes compare to other languages' value types (e.g.
struct
in C#, Swift or even plain-old C)? I see there are a lot of similarities between the two concepts (let's say one is
struct
in Swift): they can implement interfaces/protocols, can have properties, methods, and both are allocated on the stack without any memory overhead (please correct me if I'm wrong). What are the differences other than Inline classes can only have one underlying primitive type?
d

Dominaezzz

01/22/2019, 8:13 PM
At the moment, they can't have a non-public constructor.
Arrays of inline classes require boxing.
o

orangy

01/22/2019, 8:24 PM
can only have one underlying primitive type
not necessary primitive
g

ghedeon

01/22/2019, 9:10 PM
allocated on the stack
why on the stack? Isn't it heap most of the time?
k

karelpeeters

01/22/2019, 9:35 PM
Yes, normally objects are allocated on the heap. The point is that these structures don't do any heap allocation.
z

zokipirlo

01/23/2019, 8:17 AM
@ghedeon primitive types are on stack, objects are on heap... if I'm not wrong.
g

ghedeon

01/23/2019, 8:19 AM
how awesome is that, right? 🙂 Just not related) Inner classes are not about primitive types.