Luis Daivid
08/17/2020, 9:43 AMindexOfOrNull
function in Kotlin? According to Roman Elizarov, Null is the cleanest and most obvious way to indicate that there is no value. But There is only indexOf
function that return -1 in kotlin.Michael de Kaste
08/17/2020, 10:09 AMfun <T> Iterable<T>.indexOfOrNull(element: T) = indexOf(element).takeIf{ it >= 0}
deactivateduser
08/17/2020, 10:10 AMLuis Daivid
08/17/2020, 10:21 AMlouiscad
08/17/2020, 11:33 AMMichael de Kaste
08/17/2020, 11:45 AMlouiscad
08/17/2020, 11:47 AMtakeIf { it >= 0 }
or takeIf { it != -1 }
if you need to have null instead of -1.Zach Klippenstein (he/him) [MOD]
08/17/2020, 3:15 PM