Kotlin Value Classes: Stop Wrapping Everything in Regular Classes
['Baljeet Singh']
ProAndroidDev - Medium
Kotlin Value Classes: Stop Wrapping Everything in Regular Classes Baljeet Singh 5 min read · 4 hours ago 4 hours ago -- Listen ShareI’ve been working with Kotlin for a few years now, and one feature that really changed how I write code is value classes.
Enter Value ClassesKotlin introduced us to value classes.
It sees these value classes and thinks “I can optimize this away.” Your UserId(123) becomes just 123 in the final bytecode.
Here are cases where I don’t use them:Multiple properties: Value classes can only wrap a SINGLE value.
Inheritance: Value classes can’t extend other classes or be extended.