Need to return an immutable variable number of ite...
# codereview
d
Need to return an immutable variable number of items from a function. Should I return a
List<T>
or
Array<T>
?
l
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
I should also mention I'm passing full ownership to the caller. So if they wish to mutate it, so be it.
l
full ownership of what?
d
Of the
List<T>
or
Array<T>
.
l
And what it means, concretely?
d
Just emphasizing that when I say immutable, it doesn't matter 😅 . I'm not trying to protect the returned values at all.
l
Then, if iteration performance is critical or is a hot spot, use
Array
, otherwise, use
List
.
d
Okay
m
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