singleOrNull - what's your expectation?

People who expect this probably think that “single or null” stands for “the collection is allowed to have a single or null elements”.

But that is not the meaning of “orNull”. In the stdlib “orNull” generally means, returns null instead of throwing an exception. Hence “singleOrNull” would stand for “the collection is allowed to have 1 element, otherwise null is returned.”

4 Likes