Skip to content

Instantly share code, notes, and snippets.

@rohitsat13
Last active January 21, 2021 22:01
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 rohitsat13/6eac5886bf05931add6f4debb5a01fd9 to your computer and use it in GitHub Desktop.
Save rohitsat13/6eac5886bf05931add6f4debb5a01fd9 to your computer and use it in GitHub Desktop.
/* Copyright 2021 Google LLC.
SPDX-License-Identifier: Apache-2.0 */
data class MyData(
- val nums: List<Int>
+ val nums: PersistentList<Int>
)
- val myInts = mutableListOf(1, 2, 3, 4)
- val myObj = MyData(myInts)
- myInts.add(5) // Fails to compile with PersistentList, but mutates with List
+ val newData = myObj.copy(
+ nums = myObj.nums.mutate { it += 5 } // Mutate returns a new PersistentList
+ )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment