It’s a hard one. It is easier to state what I don’t expect
- Throw an exception of there are no elements
- Return any of the elements if
size>1
- Behave fundamentally different from
single()
- Throw an exception (It is the exception free version after all)
- Duplicate
firstOrNull()
Based upon those requirements the current behaviour is the only valid behaviour, but I do see how it is also confusing. The problem I see is that if the two abnormal cases (0 or >1 elements) may need to be handled differently, in that case it is not possible to have a single function do so (unless it is higher order). In the case that more than 1 element is an error, there is only one thing to it, test that. Perhaps with an extension function that explicitly throws an exception when there are multiple elements.