https://kotlinlang.org logo
Title
d

Dominaezzz

04/28/2019, 1:32 PM
Need to return an immutable variable number of items from a function. Should I return a
List<T>
or
Array<T>
?
l

louiscad

04/28/2019, 3:26 PM
Array
is always mutable but its size is fixed.
List
is read-only, but if it's backed by a
MutableList
, or another implementation that can change, its size and content may change. The simple answer is to use
List
.
d

Dominaezzz

04/28/2019, 3:27 PM
I should also mention I'm passing full ownership to the caller. So if they wish to mutate it, so be it.
l

louiscad

04/28/2019, 3:30 PM
full ownership of what?
d

Dominaezzz

04/28/2019, 3:31 PM
Of the
List<T>
or
Array<T>
.
l

louiscad

04/28/2019, 3:31 PM
And what it means, concretely?
d

Dominaezzz

04/28/2019, 3:33 PM
Just emphasizing that when I say immutable, it doesn't matter 😅 . I'm not trying to protect the returned values at all.
l

louiscad

04/28/2019, 3:38 PM
Then, if iteration performance is critical or is a hot spot, use
Array
, otherwise, use
List
.
d

Dominaezzz

04/28/2019, 3:40 PM
Okay
m

myanmarking

05/03/2019, 3:44 PM
just use list, even if it's backed by a mutablelist, the exposed type should be list. arrays are easier to mess up from the caller point of view