Google is committed to advancing racial equity for Black communities. See how.

Immutable

@Target([AnnotationTarget.CLASS]) @StableMarker annotation class Immutable
androidx.compose.Immutable

Immutable can be used to mark class as producing immutable instances. The immutability of the class is not validated and is a promise by the type that all publicly accessible properties and fields will not change after the instance is constructed. This is a stronger promise than val as it promises that the value will never change not only that values cannot be changed through a setter.

Immutable is used by composition which enables composition optimizations that can be performed based on the assumption that values read from the type will not change. See StableMarker for additional details.

data classes that only contain val properties that do not have custom getters can safely be marked as Immutable if the types of properties are either primitive types or also Immutable:

import androidx.ui.foundation.Text
import androidx.ui.layout.Row
import androidx.ui.layout.Column

@Immutable
data class Person(val name: String, val phoneNumber: String)

@Composable
fun PersonView(person: Person) {
    Column {
        Row {
            Text("Name: ")
            Text(person.name)
        }
        Row {
            Text("Phone: ")
            Text(person.phoneNumber)
        }
    }
}

@Composable
fun PeopleView(people: List<Person>) {
    Column {
        for (person in people) {
            PersonView(person)
        }
    }
}
Marking Person immutable allows calls the PersonView Composable function to be skipped ifit is the same person as it was during the last composition.

Summary

Public constructors

Immutable can be used to mark class as producing immutable instances.

Public constructors

<init>

Immutable()

Immutable can be used to mark class as producing immutable instances. The immutability of the class is not validated and is a promise by the type that all publicly accessible properties and fields will not change after the instance is constructed. This is a stronger promise than val as it promises that the value will never change not only that values cannot be changed through a setter.

Immutable is used by composition which enables composition optimizations that can be performed based on the assumption that values read from the type will not change. See StableMarker for additional details.

data classes that only contain val properties that do not have custom getters can safely be marked as Immutable if the types of properties are either primitive types or also Immutable:

import androidx.ui.foundation.Text
import androidx.ui.layout.Row
import androidx.ui.layout.Column

@Immutable
data class Person(val name: String, val phoneNumber: String)

@Composable
fun PersonView(person: Person) {
    Column {
        Row {
            Text("Name: ")
            Text(person.name)
        }
        Row {
            Text("Phone: ")
            Text(person.phoneNumber)
        }
    }
}

@Composable
fun PeopleView(people: List<Person>) {
    Column {
        for (person in people) {
            PersonView(person)
        }
    }
}
Marking Person immutable allows calls the PersonView Composable function to be skipped ifit is the same person as it was during the last composition.

See Also