for the sample, “singletonReadOnlyList”, and for t...
# announcements
m
for the sample, “singletonReadOnlyList”, and for the documentation: “..an immutable list containing only..”
🤔 1
a
Those do not return the same kind of
List
. without any elements, you get an immutable
EmptyList
, with a single element you get
java.util.Collections.singletonList
and with multiple elements you get an ordinary
ArrayList
first two actually return an immutable list, the last one returns a read-only
List
that is backed by a
MutableList
and thus can be casted into
ArrayList
and mutated
m
so then, to be accurate when we write listOf(1), we should say the list is immutable, and listOf(1,2,3) we should say it is read-only
k
Well as long as you respect the type system they're both immutable.
Actually the list you get from
listOf(1,2,3)
is a
java.util.Arrays.ArrayList
which has a fixed size but is mutable.
But you shouldn't be concerned about that, it's an implementation detail that can change and probably already does between platforms.
m
How so? the terminology is part of the public api’s documentation
a
@mutexkid because the contract is, that you get something that implements
List
, no one guarantees that it stays this way for eternity
k
Right, if you respect the type system (which is always the assumption) you'll never be able to tell the difference between "immutable" and "read-only".
m
@karelpeeters So it sounds like you’re in favor of using the terms interchangeably? If i call listOf, it doesn’t matter what we call what comes back?
k
No there's definitely a difference between those terms. The point is that the instance you get back from
listOf
should be considered immutable.
m
then I think the documentation should use that term consistently - calling it immutable if we are expected to consider it immutable
if its an implementation detail : read-only list or immutable list, great - but if we are to consider a list a certain way it needs to be talked about in that way consistently.
k
I tried to find some edge cases,
listOf(*arr)
calls
Arrays.toList
but the spread operator messes that up, see my recent question in #C0922A726. So I agree, the wording should be consistent.