Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does the expert change MutableList to List?

Tags:

kotlin

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)
like image 268
HelloCW Avatar asked Jan 20 '18 06:01

HelloCW


People also ask

What is difference between list and MutableList in Kotlin?

These are some important points you should know before working with Kotlin MutableList: List is read-only (immutable), you cannot add or update items in the original list. MutableList inherites List and supports read/write access, you can add, update or remove items.

How do I add a list to Kotlin MutableList?

To add an element to a Mutable List in Kotlin, we can use add(element), or add(index, element) functions. add(element) adds element to the end of this Mutable List. add(index, element) adds element to this Mutable List at the given index.

What is MutableList in Kotlin?

Kotlin MutableList is an interface and generic collection of elements. MutableList interface is mutable in nature. It inherits form Collection<T> class. The methods of MutableList interface supports both read and write functionalities.


1 Answers

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)
like image 101
s1m0nw1 Avatar answered Oct 15 '22 09:10

s1m0nw1