Skip to content
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

IterableExtension sortedBy doesn't permit sorting by int without workaround #271

Closed
trinarytree opened this issue Feb 22, 2023 · 5 comments · May be fixed by #315
Closed

IterableExtension sortedBy doesn't permit sorting by int without workaround #271

trinarytree opened this issue Feb 22, 2023 · 5 comments · May be fixed by #315
Labels
type-question A question about expected behavior or functionality

Comments

@trinarytree
Copy link

import 'package:collection/collection.dart';

void main() {
  [2, 1].sortedBy((i) => i);
}

yields

Error: Inferred type argument 'int' doesn't conform to the bound
'Comparable<K>' of the type variable 'K' on 'IterableExtension|sortedBy'.

sorting by an int is a pretty important use case.
the problem seems to be that sortedBy insists that its generic param K extend Comparable<K>
and int doesn't extend Comparable<int>. rather, it extends Comparable<num>.

workaround:

  [2, 1].sortedBy<num>((i) => i);

the workaround doesn't seem like it should be the end of this story.
i feel like the first example should just work. perhaps one could see this as a problem with the type inference system,
as in, why can't it figure out that K perhaps should be bound to a super class (num) of the exact type (int)
returned by the keyOf function?

@natebosch natebosch added the type-question A question about expected behavior or functionality label Feb 23, 2023
@natebosch
Copy link
Member

Closing as a duplicate of dart-lang/sdk#43763

I don't think there is anything we can do from the package side.

@eseidel
Copy link

eseidel commented Oct 10, 2023

I think simply documenting this in the sortedBy docs would go a long way. :)

@natebosch
Copy link
Member

I think simply documenting this in the sortedBy docs would go a long way

Why sortedBy but not isSortedby, binarySearchBy, lowerBoundBy, or sortBy?

@eseidel
Copy link

eseidel commented Oct 10, 2023

I think simply documenting this in the sortedBy docs would go a long way

Why sortedBy but not isSortedby, binarySearchBy, lowerBoundBy, or sortBy?

No good reason. Was mostly trying to avoid "perfect being the enemy of the good" by documenting the one method through which myself and two other colleagues had independently encountered this error. 🤣 Happy to see something more systemic, just was trying to be helpful. I've personally not used any of the 4 other methods, but I have no data to say which are more/less common.

@natebosch
Copy link
Member

I think if we do decide to address this with a note in the doc comment we can easily enough make it a template and repeat it in each impacted method.

Once we hear back from the analyzer team on the feasibility for making the diagnostic more informative we can make a choice on landing the PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-question A question about expected behavior or functionality
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants