dG
11/11/2019, 10:30 AMif (someArray[x] != null) { someArray[x].add(foo) }
Why does the compiler not recognize that someArray[x]
cannot be null, and insists on !!
?karelpeeters
11/11/2019, 10:33 AMsomeArray[x]
is not null?
The compiler is very conservative about smartcasting, for example in this case another thread might have modified the array inbetween.marstran
11/11/2019, 10:36 AMsomeArray[x]?.let { it.add(foo) }
.dG
11/11/2019, 11:45 AMsomeArray
right before the if statement. Would checking if other threads have access to the variable not be an option for the compiler?dG
11/11/2019, 11:46 AMelse
(where someArray[x] = mutableList()
)marstran
11/11/2019, 11:48 AMval list = someArray[x] ?: mutableListOf()
list.add(foo)
someArray[x] = list
dG
11/11/2019, 11:53 AMthymecypher
11/11/2019, 1:30 PMpublic operator fun get(index: Int): E
Since it returns a non-nullable type, you will only ever receive the object at the given index or I believe a IndexOutOfBoundsException
- it’s equal to someArray.get(<index>)
To get a null from the accessor you need to either use
list.getOrNull(index)
or
List<(your type)?>
The first, you will get null if the index is out of bounds or if the type is nullable AND the value at the given index is null.
The second, you will still get an out of bounds error if the index is out of bounds but if the value at that index is null, you will get a null.
The second should be avoided - as it’s best practices to avoid nullable types whenever possible.thymecypher
11/11/2019, 1:36 PMsomeArray[x]
if someArray
is at least x long.
For example,
val list = mutableListOf(1,2,3)
list[3] = 2
will produce an IndexOutOfBoundsException
karelpeeters
11/11/2019, 2:01 PMArray<T?>
and the fact that the compiler doesn't smartcast elements.Derek Peirce
11/12/2019, 3:27 AMsomeArray
right before this statement, the compiler doesn't know if the method used to generate someArray
kept a reference to that array and is mutating it in another thread. It was probably a library method that would never do something like that, but the compiler shouldn't know that.