https://kotlinlang.org logo
Title
g

gsala

03/01/2023, 1:37 PM
What do you think of this ImmutableList class definition?
class ImmutableList<T>(list: List<T>) : List<T> by list.toList()
l

LeoColman

03/01/2023, 2:50 PM
I don't think it's a given that
toList
will return you a new list
So maybe it's not immutable?
l

Loney Chou

03/01/2023, 4:11 PM
toList
will give you a new
ArrayList
c

Chris Fillmore

03/02/2023, 2:25 AM
g

gsala

03/03/2023, 10:28 AM
@LeoColman good point, it depends on the implementation of
toList
. What about declaring it as:
class ImmutableList<T>(list: List<T>) : List<T> by ArrayList(list)
we miss the empty list and singletonlist optimizations from inside
toList
but that’s probably alright