Sam
01/24/2023, 3:58 PMif (iterator.hasNext()) doSomethingWith(iterator.next()) ?: return null else return null
I have very mixed feelings about return null else return null
😄ephemient
01/24/2023, 4:25 PM(if (iterator.hasNext()) doSomethingWith(iterator.next()) else null) ?: return null
or if it's an iterator of non-null,
iterator.takeIf { it.hasNext() }?.let { doSomethingWith(it.next()) } ?: return null
ephemient
01/24/2023, 4:25 PMIterator<T>.nextOrNull()
could be useful on a handful of occasions, but not very oftenSam
01/24/2023, 4:34 PMtakeIf { it.hasNext() }
is a nice idea, I'll have to have a play with that 👌janvladimirmostert
01/24/2023, 9:53 PMephemient
01/24/2023, 10:01 PM!!
in some situations, such as when providing a detailed message, because Kotlin doesn't. https://youtrack.jetbrains.com/issue/KT-6989
but that doesn't apply to that code snippet 😞Sam
01/24/2023, 10:26 PMgetValue
too, but I'd have to check how useful its message is. I've never been quite sure whether it's actually intended to be used at all on non-defaulted maps...ephemient
01/24/2023, 10:29 PMgetValue
throws NoSuchElementException("Key $key is missing in the map.")
, so it is useful