Is there function like Collection::singleOrNull bu...
# stdlib
p
Is there function like Collection::singleOrNull but throws exception if there is more then 1 element matching selector? Current one is returning null for both 0 and >1 cases.
a
.single()
p
.single() throws exception in case of 0 matches, but I need null in case of 0 matches
j
Do a count{} and then check <= 1
p
I wonder if there is stdlib solution for this case.
a
You could do
collection.ifEmpty { null }?.single()
otherwise i don’t think there’s a builtin way to do that
p
Any reason that this case is not implemented in stdlib?
i
Lack of a good name
👍 1
p
singleOrNullIfNone?