What's the difference between `find { }` and `firs...
# announcements
d
What's the difference between
find { }
and
firstOrNull { }
on
Iterable<T>
in the stdlib?
g
a
From source code:
Copy code
/**
 * Returns the first element matching the given [predicate], or `null` if no such element was found.
 */
@kotlin.internal.InlineOnly
public inline fun <T> Iterable<T>.find(predicate: (T) -> Boolean): T? {
    return firstOrNull(predicate)
}
hence find is just an alias
d
Interesting discussion over there... so most people would use
find
?
a
it depends. find is shorter, but in some cases firstOrNull may be more "expressive" in terms of readability.
for example from code context you are dealing with list of unique elements then it makes sense to use find. in case of list which may have duplicates firstOrNull now makes more sense.
d
That's why I was expecting
Set<T>.find
... I was surprised it was on
Iterable
. Thanks for the confirmation 🙂!