Michael de Kaste
06/16/2023, 7:39 AMorNull
functions in mind, I was unpleasantly surprised there is no such thing as List<T>.indexOfFirstOrNull
Sam
06/16/2023, 7:48 AMnull
isn’t really much more useful than -1
. Personally, I’m with you; I would much prefer null
. An unexpected -1
can easily go unnoticed, leading to errors down the line. Whereas the type system forces you to handle null
, so if there’s a chance my index won’t exist, I definitely want that. I suppose the downside of using null
is that the return type would have to be boxed. But since we’re only dealing with a single value here, I don’t see that being a big concern.Michael de Kaste
06/16/2023, 7:48 AMMichael de Kaste
06/16/2023, 7:52 AMMichael de Kaste
06/16/2023, 7:52 AMindexOfFirst
Derek Peirce
06/16/2023, 7:55 AMInt
where you'd either clearly assert that you're getting a useful integer or have to write a case for when the item is not found, maybe value class Index(private val i: Int)
.mcpiroman
06/16/2023, 8:13 AMlist.indexOfFirst(foo).takeUnless { it == -1 }