Skip to content

Cannot use int type with the sortedBy API #669

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

Closed
etiennestuder opened this issue Jan 6, 2024 · 3 comments
Closed

Cannot use int type with the sortedBy API #669

etiennestuder opened this issue Jan 6, 2024 · 3 comments

Comments

@etiennestuder
Copy link

Using Dart 3.2.4 and the collections package, and writing the code below...

['alpha','beta'].sortedBy((element) => element.length);

I get the error printed below:

Error: Inferred type argument 'int' doesn't conform to the bound 'Comparable<K>' of the type variable 'K' on 'IterableExtension|sortedBy'.
 - 'Comparable' is from 'dart:core'.
Try specifying type arguments explicitly so that they conform to the bounds.
    var xx = ['alpha','beta'].sortedBy((element) => element.length);

Does it mean that int does not implement Comparable?
I did not know how to apply the advice given in the error message.

I'm not a Dart expert, and it is unclear to me whether sorting by a criteria of type int is not supported by design when using the sortedBy API, or if I'm doing something wrong, or if there is an issue in the library.

@etiennestuder
Copy link
Author

I found the following issue and solution:

['alpha', 'beta'].sortedBy<num>((element) => element.length);

@mosuem mosuem transferred this issue from dart-archive/collection Oct 21, 2024
@eernstg
Copy link
Member

eernstg commented Dec 15, 2024

Note that the example works with Dart 3.7.0 and up because inference was generalized to use bounds more effectively:

import 'package:collection/collection.dart';

void main() {
  print(['alpha', 'beta'].sortedBy((element) => element.length));
}

@etiennestuder
Copy link
Author

Cool. Thanks for the heads-up, @eernstg.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants