ifWithinBound

inline fun <E, R> List<E>.ifWithinBound(index: Int, thenBlock: () -> R, elseBlock: () -> R): R(source)
inline fun <E, R> Array<E>.ifWithinBound(index: Int, thenBlock: () -> R, elseBlock: () -> R): R(source)

Checks if the given index is within the bound of this List (index<List.size) and correspondingly executes the thenBlock or the elseBlock.

Use getOrElse in case you want to retrieve the element at position index or a default value. This function is meant for other things such as check if upper bound of range is within bound and return a sublist or return an empty list etc.

Return

The result of the thenBlock or the elseBlock.

Parameters

E

Element type of this List.

R

Return type.

index

The index which is used for the bound check.

thenBlock

The block which is executed if the index is within the bound.

elseBlock

The block which is executed if the index is outside the bound.