Cross references in type parameters

Kotlin doesn’t have raw types, you cannot just drop the type parameters. One option similar to raw types is to use star projections: abstract class Element<S : Snapshot<*>> { /* … */ } abstract class Snapshot<E : Element<*>> { /* … */ } But you won’t be able to normally work with the type parameters … Read more

Override getter for Kotlin data class

After spending almost a full year of writing Kotlin daily I’ve found that attempting to override data classes like this is a bad practice. There are 3 valid approaches to this, and after I present them, I’ll explain why the approach other answers have suggested is bad. Have your business logic that creates the data … Read more

How to clone object in Kotlin?

For a data class, you can use the compiler-generated copy() method. Note that it will perform a shallow copy. To create a copy of a collection, use the toList() or toSet() methods, depending on the collection type you need. These methods always create a new copy of a collection; they also perform a shallow copy. … Read more

IntArray vs Array in Kotlin

Array<Int> is an Integer[] under the hood, while IntArray is an int[]. That’s it. This means that when you put an Int in an Array<Int>, it will always be boxed (specifically, with an Integer.valueOf() call). In the case of IntArray, no boxing will occur, because it translates to a Java primitive array. Other than the … Read more

Why does mutableStateOf without remember work sometimes?

It’s a feature of Compose about scoping and smart recomposition. You can check my detailed answer here. What really makes your whole Composable to recompose is Column having inline keyword. @Composable inline fun Column( modifier: Modifier = Modifier, verticalArrangement: Arrangement.Vertical = Arrangement.Top, horizontalAlignment: Alignment.Horizontal = Alignment.Start, content: @Composable ColumnScope.() -> Unit ) { val measurePolicy … Read more

Getters and Setters in Kotlin

Getters and setters are auto-generated in Kotlin. If you write: val isEmpty: Boolean It is equal to the following Java code: private final Boolean isEmpty; public Boolean isEmpty() { return isEmpty; } In your case the private access modifier is redundant – isEmpty is private by default and can be accessed only by a getter. … Read more