为什么应该始终使用不可变集合接口来声明在 Kotlin 中不发生变化的集合?
Why should Always use immutable collection interfaces to declare collections which are not mutated in Kotlin?
我正在阅读 Kotlin 文档。在 Immutability 部门,他们在下面发表评论。我想知道我们为什么要这样做?当我尝试示例代码时,它的行为是一样的。
Immutability
Prefer using immutable data to mutable. Always declare local variables
and properties as val rather than var if they are not modified after
initialization.
Always use immutable collection interfaces ( Collection , List , Set ,
Map ) to declare collections which are not mutated. When using factory
functions to create collection instances, always use functions that
return immutable collection types when possible:
// Bad: use of mutable collection type for value which will not be mutated
fun validateValue(actualValue: String, allowedValues: HashSet<String>) { ... }
// Good: immutable collection type used instead
fun validateValue(actualValue: String, allowedValues: Set<String>) { ... }
// Bad: arrayListOf() returns ArrayList<T>, which is a mutable collection type
val allowedValues = arrayListOf("a", "b", "c")
// Good: listOf() returns List<T>
val allowedValues = listOf("a", "b", "c")
更新:致所有投反对票的人。在写这个问题之前,我阅读了这本书,尝试了示例并尝试进行搜索。所以我没有足够的经验来解释或理解上面的段落。让我们考虑一下您对这个社区的贡献。如果我做错了,请告诉我。不要只点击一个按钮。
根据@Akavall先生和@Naetmul先生的建议。我再次阅读了 listOf
方法的文档。问题是我错过了 listOf
方法 returns 无法添加任何新项目的列表对象。
I think you should compare MutableList and List. Both are
interfaces, whereas ArrayList is a concrete class from Java.
ArrayList implements MutableList in Kotlin. MutableList
extends List, adding mutability functionalities which are not in
List originally.
我正在阅读 Kotlin 文档。在 Immutability 部门,他们在下面发表评论。我想知道我们为什么要这样做?当我尝试示例代码时,它的行为是一样的。
Immutability
Prefer using immutable data to mutable. Always declare local variables and properties as val rather than var if they are not modified after initialization.
Always use immutable collection interfaces ( Collection , List , Set , Map ) to declare collections which are not mutated. When using factory functions to create collection instances, always use functions that return immutable collection types when possible:
// Bad: use of mutable collection type for value which will not be mutated fun validateValue(actualValue: String, allowedValues: HashSet<String>) { ... } // Good: immutable collection type used instead fun validateValue(actualValue: String, allowedValues: Set<String>) { ... } // Bad: arrayListOf() returns ArrayList<T>, which is a mutable collection type val allowedValues = arrayListOf("a", "b", "c") // Good: listOf() returns List<T> val allowedValues = listOf("a", "b", "c")
更新:致所有投反对票的人。在写这个问题之前,我阅读了这本书,尝试了示例并尝试进行搜索。所以我没有足够的经验来解释或理解上面的段落。让我们考虑一下您对这个社区的贡献。如果我做错了,请告诉我。不要只点击一个按钮。
根据@Akavall先生和@Naetmul先生的建议。我再次阅读了 listOf
方法的文档。问题是我错过了 listOf
方法 returns 无法添加任何新项目的列表对象。
I think you should compare MutableList and List. Both are interfaces, whereas ArrayList is a concrete class from Java. ArrayList implements MutableList in Kotlin. MutableList extends List, adding mutability functionalities which are not in List originally.