04/03/2018, 5:31 AM
but that maybe requires a lot of clone and copy code, and that hurts performance as well
Also you can return some immutable inteface instead of mutable class to avoid copy. Like:
Copy code
// Immutable interface, only val
interface Foo {
   val bar: Bar
// Mutable version
class FooImpl(override var: bar: Bar) : Foo

// Now we can just expose read only version of List with Foo
private val myMutableList = mutableListOf<FooImpl>()
val list: List<Foo>
      get() = myMutableList // Or myMutableList.toList()
It’s not 100% protection, someone can cast List to ArrayList (if you don’t use toList()) or cast Foo to FooImpl, but it’s good enough in most cases, especially for your own code