mirror of
https://github.com/dart-lang/sdk
synced 2024-10-01 19:19:16 +00:00
Add compareByIndex
helper function to Enum
.
Fixes https://github.com/dart-lang/language/issues/1836 Bug: https://github.com/dart-lang/language/issues/1836 Change-Id: I43ef26403a379c795a0bdcdeb470b4818ffb721e Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/212573 Commit-Queue: Lasse R.H. Nielsen <lrn@google.com> Reviewed-by: Nate Bosch <nbosch@google.com>
This commit is contained in:
parent
e995cb5f7c
commit
39a165647a
|
@ -78,6 +78,10 @@
|
|||
- The experimental `waitFor` functionality, and the library containing only that
|
||||
function, are now deprecated.
|
||||
|
||||
### `dart:core`
|
||||
|
||||
- Add `Enum.compareByIndex` helper function for comparing enum values by index.
|
||||
|
||||
### Tools
|
||||
|
||||
#### Dart command line
|
||||
|
|
|
@ -20,4 +20,14 @@ abstract class Enum {
|
|||
/// This is also the index of the value in the
|
||||
/// enumerated type's static `values` list.
|
||||
int get index;
|
||||
|
||||
/// Compares two enum values by their [index].
|
||||
///
|
||||
/// A generic [Comparator] function for enum types which
|
||||
/// orders enum values by their [index] value, which corresponds
|
||||
/// to the source order of the enum element declarations in
|
||||
/// the `enum` declaration.
|
||||
@Since("2.15")
|
||||
static int compareByIndex<T extends Enum>(T value1, T value2) =>
|
||||
value1.index - value2.index;
|
||||
}
|
||||
|
|
|
@ -93,6 +93,21 @@ main() {
|
|||
Expect.type<Enum>(Enum1._);
|
||||
Enum enumValue = Enum1._;
|
||||
Expect.equals(0, enumValue.index);
|
||||
|
||||
// Enum.compareByIndex orders enums correctly.
|
||||
var enumValues = [Enum5.G, Enum5.H, Enum5.F];
|
||||
for (var i = 0; i < 10; i++) {
|
||||
enumValues.sort(Enum.compareByIndex);
|
||||
enumValues.fold<int>(-1, (previousValue, element) {
|
||||
Expect.isTrue(previousValue < element.index, "$enumValues");
|
||||
return element.index;
|
||||
});
|
||||
enumValues.shuffle();
|
||||
}
|
||||
// Can be used at the type `Enum` to compare different enums.
|
||||
Expect.isTrue(Enum.compareByIndex<Enum>(Enum4.D, Enum5.H) < 0);
|
||||
Expect.isTrue(Enum.compareByIndex<Enum>(Enum4.E, Enum5.F) > 0);
|
||||
Expect.isTrue(Enum.compareByIndex<Enum>(Enum4.D, Enum5.F) == 0);
|
||||
}
|
||||
|
||||
test1(Enum1 e) {
|
||||
|
|
|
@ -95,6 +95,21 @@ main() {
|
|||
Expect.type<Enum>(Enum1._);
|
||||
Enum enumValue = Enum1._;
|
||||
Expect.equals(0, enumValue.index);
|
||||
|
||||
// Enum.compareByIndex orders enums correctly.
|
||||
var enumValues = [Enum5.G, Enum5.H, Enum5.F];
|
||||
for (var i = 0; i < 10; i++) {
|
||||
enumValues.sort(Enum.compareByIndex);
|
||||
enumValues.fold<int>(-1, (previousValue, element) {
|
||||
Expect.isTrue(previousValue < element.index, "$enumValues");
|
||||
return element.index;
|
||||
});
|
||||
enumValues.shuffle();
|
||||
}
|
||||
// Can be used at the type `Enum` to compare different enums.
|
||||
Expect.isTrue(Enum.compareByIndex<Enum>(Enum4.D, Enum5.H) < 0);
|
||||
Expect.isTrue(Enum.compareByIndex<Enum>(Enum4.E, Enum5.F) > 0);
|
||||
Expect.isTrue(Enum.compareByIndex<Enum>(Enum4.D, Enum5.F) == 0);
|
||||
}
|
||||
|
||||
test1(Enum1 e) {
|
||||
|
|
Loading…
Reference in a new issue