https://kotlinlang.org logo
l

Lilly

01/12/2021, 3:52 AM
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

Matt Rea

01/12/2021, 6:19 AM
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

ursus

01/12/2021, 5:11 PM
depends on how much you trust your consumers, it might be okay just to cast
l

Lilly

01/13/2021, 4:06 PM
ok, Thanks guys :)