Eugen Mayer
02/07/2025, 9:00 AMKlitos Kyriacou
02/07/2025, 12:18 PMval mutableList = (returnedList as? MutableList) ?: returnedList.toMutableList()
so that you only make the copy if the list is not already mutable. But if this list comes from Java and is wrapped in Collections.unmodifiableList
then this unmodifiableList will still be seen as a MutableList
in Kotlin but you will get an exception at runtime if you try to modify it.
In any case, are you sure that the returned list is safe to modify? Are there any risks related to multithreading? Does the Java side that returned that list to you not want the contents of that list any more? Given such considerations, the Java method that returns the list needs to specify whether it's safe to modify that list or not - in which case, you won't have this problem any more.