5

I asked a question at How to design a complex class which incude some classes to make expansion easily in future in Kotlin? about how to design a complex class which incude some classes to make expansion easily in future in Kotlin.

A expert named s1m0nw1 give me a great answer as the following code.

But I don't know why he want to change MutableList to List at https://stackoverflow.com/posts/47960036/revisions , I can get the correct result when I use MutableList. Could you tell me?

The code

interface DeviceDef
data class BluetoothDef(val Status: Boolean = false) : DeviceDef
data class WiFiDef(val Name: String, val Status: Boolean = false) : DeviceDef
data class ScreenDef(val Name: String, val size: Long) : DeviceDef 


class MDetail(val _id: Long, val devices: List<DeviceDef>) {

    inline fun <reified T> getDevice(): T {
        return devices.filterIsInstance(T::class.java).first()
    }
}

Added

I think that mutableListOf<DeviceDef> is better than ListOf<DeviceDef> in order to extend in future.

I can use aMutableList.add() function to extend when I append new element of mutableListOf<DeviceDef>.

If I use ListOf<DeviceDef>, I have to construct it with listOf(mBluetoothDef1, mWiFiDef1, //mOther), it's not good. Right?

var aMutableList= mutableListOf<DeviceDef>()

var mBluetoothDef1= BluetoothDef(true)
var mWiFiDef1= WiFiHelper(this).getWiFiDefFromSystem()

aMutableList.add(mBluetoothDef1)
aMutableList.add(mWiFiDef1)

// aMutableList.add(mOther)  //This is extension

var aMDetail1= MDetail(myID, aMutableList)
HelloCW
  • 843
  • 22
  • 125
  • 310
  • 1
    you can find lots of answers just google "why immutable" – garywzh Jan 20 '18 at 07:04
  • 3
    Imagine you're an artist doing paintings and sculptures. A museum asks you to borrow some of your artwork for an exhibition, open to anyone. Wouldn't you like it better if the museum told you "people in the museum may look, but they may not touch"? Same here: Since the class is supposed to only get data from the list and not modify it in any way, you'd better make that clear by accepting a List rather than a MutableList. It also allows passing more kinds of Lists, BTW, since all lists are not mutable. – JB Nizet Jan 20 '18 at 07:15

1 Answers1

9

Sorry for not giving an explanation in the first place. The differences are explained in the docs.:

Unlike many languages, Kotlin distinguishes between mutable and immutable collections (lists, sets, maps, etc). Precise control over exactly when collections can be edited is useful for eliminating bugs, and for designing good APIs.

It is important to understand up front the difference between a read-only view of a mutable collection, and an actually immutable collection. Both are easy to create, but the type system doesn't express the difference, so keeping track of that (if it's relevant) is up to you.

The Kotlin List<out T> type is an interface that provides read-only operations like size, get and so on. Like in Java, it inherits from Collection<T> and that in turn inherits from Iterable<T>. Methods that change the list are added by the MutableList<T> interface. [...]

The List interface provides a read-only view so that you cannot e.g add new elements to the list which has many advantages for instance in multithreaded environments. There may be situations in which you will use MutableList instead.

I also recommend the following discussion: Kotlin and Immutable Collections?

EDIT (added content):

You can do this is a one-liner without any add invocation:

val list = listOf(mBluetoothDef1, mWiFiDef1)
Community
  • 1
  • 1
s1m0nw1
  • 76,759
  • 17
  • 167
  • 196