y
09/30/2024, 8:09 AMIndexOutOfBoundsException
except in Kotlin/JS where the behavior is unspecified."
does this mean I can't rely on catching an IndexOutOfBoundsException
here? (this is just curiosity, not trying to solve a specific issue)Sam
09/30/2024, 8:19 AMSam
09/30/2024, 8:19 AMgetOrElse
or getOrNull
instead of get
Sam
09/30/2024, 8:26 AMundefined
if it's out of boundsYoussef Shoaib [MOD]
09/30/2024, 8:26 AMundefined
(because JS).
Edit: Sam beat me to the punch lol!y
09/30/2024, 9:05 AMy
09/30/2024, 9:06 AMy
09/30/2024, 9:07 AMgetOrNull
- doesn't work in the case where you're indexing into a List<T?>
, and so getOrNull
returning null is ambiguous. which of course you can solve in other ways)Sam
09/30/2024, 9:19 AMy
09/30/2024, 9:22 AMJohann Pardanaud
09/30/2024, 10:06 AMindices
range:
if (index in myArray.indices) {
println(myArray[index])
}
Johann Pardanaud
09/30/2024, 10:06 AMy
09/30/2024, 10:13 AMgetOrNull
equivalent that returns one of: class Exists(val T)
, object ExistsButNull
, object OutOfRange
.
or use some general Either<A, B>
class and write a getOrNull
equivalent that returns Either<T?, Unit>
or something like that.
etc.