-
Notifications
You must be signed in to change notification settings - Fork 88
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Confusing singleWhereOrNull
extension method
#294
Comments
I can see how the I do think that |
Chiming in that the implementation of this method is confusing, for the reasons discussed here. I expect a |
Do you expect The |
Not if the suffix is
Understood, that is what it does. It's not what I expected, and I'm glad I read the docs. What you call I agree with jaripekkala that the behavior of this method is confusing, and I think it's likely that others are wrongly expecting |
I was also just confused by the difference in behavior of I would've expected both to throw when the collection has 2 or more elements, but only Thus I updated our code to an internal extension for
Yes, and I think that (for me) goes back to the I really don't get the use-case for "oh, we have 2 (or more) matching elements, let's work with none of them)", whereas "uh oh, we have more than the 1 expected element" is something I want to guard against all the time. |
This is the key point for me. Personally, I find the reasoning at #294 (comment) for why a method called But I think it's pretty rare to be writing code where that's the behavior one actually wants; I'm at a bit of a loss to think up an example. Given that use cases for the I'd suggest, in addition to adding a |
Instead of a prominent note, why not introduce an (optional) parameter like |
https://github.com/dart-lang/collection/blob/v1.17.2/lib/src/iterable_extensions.dart#L296-L304
My proposal: Deprecate and delete
singleWhereOrNull
, and introduce more clear methodsReasoning:
Many use the
singleWhere
method as replacement tofirstWhere
when the list should not contain more than one matching value, in cases when the logic would not work if code somewhere later changes in way that there are multiple matching values, so throwing error would make sense (developer need to go and handle the case).But when also no matching value is acceptable, developer can mistakenly use
singleWhereOrNull
and have different behaviour than intended. These cases are not so commonly unit tested, as these cases are preventing mistakes in the future.The text was updated successfully, but these errors were encountered: