am
03/06/2018, 1:38 PMmiszmaniac
03/06/2018, 1:39 PMpublic fun <T> Iterable<T>.toList(): List<T> {
if (this is Collection) {
return when (size) {
0 -> emptyList()
1 -> listOf(if (this is List) get(0) else iterator().next())
else -> this.toMutableList()
}
}
return this.toMutableList().optimizeReadOnlyList()
} This way:)miszmaniac
03/06/2018, 1:39 PMAndreas Sinz
03/06/2018, 1:43 PMmutable vs immutable lists. there are no real ìmmutable-lists in kotlin yet. List is a read-only view of a MutableList underneatham
03/06/2018, 1:45 PMAndreas Sinz
03/06/2018, 1:53 PMtoList() creates a mutableList and returns an object that implements List<T> which has no methods on it to change the underlying mutableList, so its a read-only ListAndreas Sinz
03/06/2018, 1:59 PMtoList only guarantees that you get an object back that implements List<T>, using mutableList() is just an implementation detailam
03/06/2018, 2:08 PM