Skip to content

Instantly share code, notes, and snippets.

@osipxd
Last active March 28, 2024 00:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save osipxd/638cc9f3b6a483177d2e9bb7c8697ca4 to your computer and use it in GitHub Desktop.
Save osipxd/638cc9f3b6a483177d2e9bb7c8697ca4 to your computer and use it in GitHub Desktop.
Transparent wrapper making lists Immutable for compose.
/** Wrapper over the given [list] for Compose to mark this list as [Immutable]. */
@Immutable
@JvmInline
value class ImmutableList<T>(private val list: List<T>) : List<T> by list
/** Returns empty list marked as [Immutable]. */
inline fun <T> immutableListOf(): ImmutableList<T> = ImmutableList(emptyList())
/** Returns a new [Immutable] list of given [elements]. */
inline fun <T> immutableListOf(vararg elements: T): ImmutableList<T> = ImmutableList(elements.asList())
/** Wraps [this] list with [ImmutableList] so Compose considers it [Immutable]. */
inline fun <T> List<T>.asImmutableList(): ImmutableList<T> = ImmutableList(this)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment