Kirill Grouchnikov
11/23/2020, 10:39 PMlistOf
creates a whole new list. Java's unmodifiableList
is a very thin wrapper that throws exceptions on trying to modify the contents of the wrapped listephemient
11/23/2020, 10:39 PMCollections.unmodifiableList()
if you're on JVMKirill Grouchnikov
11/23/2020, 10:40 PMNir
11/23/2020, 10:40 PMephemient
11/23/2020, 10:41 PMNir
11/23/2020, 10:41 PMArkadii Ivanov
11/23/2020, 10:42 PMNir
11/23/2020, 10:42 PMKirill Grouchnikov
11/23/2020, 10:43 PMMutableList
. That's the whole point here. I want a one liner that creates a thin, unmodifiable view on the original list. If there's no such thing, just say that.ephemient
11/23/2020, 10:43 PMNir
11/23/2020, 10:44 PMclass UnmodifiableList<T>(val list: List<T>) : List<T> by list
Arkadii Ivanov
11/23/2020, 10:44 PMNir
11/23/2020, 10:44 PMArkadii Ivanov
11/23/2020, 10:44 PMNir
11/23/2020, 10:44 PMKirill Grouchnikov
11/23/2020, 10:45 PMlistOf
is too heavyweight as it creates a whole new list and takes up a separate chunk of heapephemient
11/23/2020, 10:46 PMNir
11/23/2020, 10:46 PMephemient
11/23/2020, 10:47 PMArkadii Ivanov
11/23/2020, 10:47 PMKirill Grouchnikov
11/23/2020, 10:47 PMNir
11/23/2020, 10:47 PMKirill Grouchnikov
11/23/2020, 10:49 PMephemient
11/23/2020, 10:49 PMKirill Grouchnikov
11/23/2020, 10:50 PMephemient
11/23/2020, 10:50 PMVampire
11/23/2020, 11:00 PMNir
11/23/2020, 11:07 PMZach Klippenstein (he/him) [MOD]
11/23/2020, 11:09 PMobject : List<Foo> by yourListOfFoos {}
is one line and hides the underlying type like you want.Vampire
11/23/2020, 11:09 PMfun <T> List<T>.asUnmodifiableList() = object : List<T> by this { }
val unmodifiableList = mutableList.asUnmodifiableList()
ephemient
11/23/2020, 11:10 PMVampire
11/23/2020, 11:12 PMNir
11/23/2020, 11:14 PMephemient
11/23/2020, 11:19 PMZach Klippenstein (he/him) [MOD]
11/23/2020, 11:21 PMMutableList
.Nir
11/23/2020, 11:23 PMZach Klippenstein (he/him) [MOD]
11/23/2020, 11:27 PMobject :
creates a new type, and completely unrelated types can’t be cast to each other on the JVM. Unless we’re trying subtly different code?Nir
11/23/2020, 11:28 PMclass foo(val x: List<Foo>) : List<Foo> by x
Vampire
11/23/2020, 11:28 PMfun <T> List<T>.asUnmodifiableList() = object : List<T> by this {}
val mutableList = mutableListOf(1, 2, 3)
println(mutableList)
mutableList.add(4)
println(mutableList)
val unmodifiableList = mutableList.asUnmodifiableList()
println(unmodifiableList.toList())
mutableList.add(5)
println(unmodifiableList.toList())
(unmodifiableList as MutableList).add(6)
println(unmodifiableList.toList())
[1, 2, 3][1, 2, 3, 4][1, 2, 3, 4][1, 2, 3, 4, 5]java.lang.ClassCastException: Line_0$asUnmodifiableList$1 cannot be cast to kotlin.collections.MutableList
ephemient
11/23/2020, 11:31 PMNir
11/23/2020, 11:31 PMephemient
11/23/2020, 11:31 PMfun main() {
object : List<Any> by emptyList() {} as MutableList<Any>
}
Nir
11/23/2020, 11:32 PMVampire
11/23/2020, 11:33 PM[1, 2, 3]
[1, 2, 3, 4]
[1, 2, 3, 4]
[1, 2, 3, 4, 5]
Exception in thread "main" java.lang.ClassCastException: de.empic.teamcity.config.FooKt$asUnmodifiableList$1 cannot be cast to kotlin.collections.MutableList
at de.empic.teamcity.config.FooKt.main(Foo.kt:14)
at de.empic.teamcity.config.FooKt.main(Foo.kt)
From a compiled .kt fileephemient
11/23/2020, 11:34 PMNir
11/23/2020, 11:38 PMImmutableList
that defensively copies the constructor, or otherwise provides functions where it knows it's "safe" is well worth itList
member that gets modified from halfway across your program, is very very well within the realm of MurphyVampire
11/23/2020, 11:53 PMNir
11/23/2020, 11:54 PM