when using kotlinx immutable classes, is it best t...
# announcements
w
when using kotlinx immutable classes, is it best to use the
List
type for return values or
ImmutableList
? i don't want the downstream consumers to modify it (which either would provide). It kind of seems like an implementatino details that i am using
ImmutableList
n
You should definitely use
ImmutableList
, IMHO
the problem is that if you use
List
, there is no way to know if that's a read-only view of a potentially-mutable class, or a read-only view of an immutable class
e.g. if you have `data class foo(val x: ImmutableList<Int>)`; you can't construct
foo
from a
List
, it wouldn't be safe. So, if you have a function that returns
List
, and you wanted to construct
foo
from it, you'd need basically perform a defensive copy to get your
ImmutableList
IMHO the standard library suffers extensively from this problem, actually. You have things like
map
which return
List
but should really return
ImmutableList
.
w
ah ok i see. some good points