アウスジョン(アウス)
11/25/2023, 10:47 AMmyDataClass = myDataClass.copy(myInt = newIntValue)
But say I have
MyDataClass(
val listOfInt: List<Int>
)
and I want to update myDataClass.listOfInt[5] ?David Kubecka
11/25/2023, 11:09 AMMutableList and do myDataClass.listOfInt[5] = 42
• Keep using immutable List. Unfortunately, there's no native way to make a list copy with an updated element, so you will need to create a temporary mutable copy of it, e.g. with this extension
fun <T> List<T>.update(index: Int, item: T): List<T> = toMutableList().apply { this[index] = item) }
With that you can write
myDataClass.copy(listOfInt = myDataClass.listOfInt.update(5, 42))アウスジョン(アウス)
11/25/2023, 11:20 AMMutableList and not using copy?bram
11/25/2023, 11:25 AMO(N) operation vs O(1)アウスジョン(アウス)
11/25/2023, 11:27 AMbram
11/25/2023, 11:31 AMアウスジョン(アウス)
11/25/2023, 11:32 AMbram
11/25/2023, 11:33 AMアウスジョン(アウス)
11/25/2023, 11:33 AMbram
11/25/2023, 11:33 AMbram
11/25/2023, 11:34 AMbram
11/25/2023, 11:34 AMbram
11/25/2023, 11:35 AMbram
11/25/2023, 11:35 AMbram
11/25/2023, 11:41 AMDavid Kubecka
11/25/2023, 11:42 AMbram
11/25/2023, 11:42 AMephemient
11/25/2023, 4:32 PMephemient
11/25/2023, 4:34 PMPersistentList, which is immutable with efficient copy (using sharing)