Kotlin tips #1 : Avoid ArrayOutOfBoundException
1 min readSep 9, 2018
Hey ! I found a very useful Kotlin function recently :
No need to check by hand if the position is within the array bound, now you can process like below :
Okay, let me show you another one. Now what if you want to put fruits[position] in a val, and want a default value if the position is out of bound ?
Here is what is gives with our previous example:
As you can see, the second argument is a lambda function, allowing you to do something depending on the position:
I hope this will helps you to write more concise code with Kotlin !
Cheers