Hi maybe a stupid question but I never thought abo...
# android
l
Hi maybe a stupid question but I never thought about that before. When I define a
mutableListOf<String>
and pass this to a consuming function, should I convert this list explicitly to
List<String>
with
toList()
to have this list immutable or is it enough to define the parameter as
List<String>
like:
Copy code
fun consumingList(list: List<String>)
But then I'm asking if it's really an immutable list... I could still cast it to a mutable List again, not?
m
Defining the parameter like that is fine. It’s all mutable under the hood.
List
just exposes less than
MutableList
. And like you mentioned, you can use
toList()
or
toMutableList()
to convert between them
u
depends on how much you trust your consumers, it might be okay just to cast
l
ok, Thanks guys :)