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

fix: props are not of type Comparable #149

Merged
merged 1 commit into from Aug 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 2 additions & 6 deletions lib/src/equatable_utils.dart
@@ -1,5 +1,3 @@
import 'dart:collection';

import 'package:collection/collection.dart';
import 'package:equatable/equatable.dart';

Expand Down Expand Up @@ -48,10 +46,8 @@ int _combine(int hash, dynamic object) {
});
return hash;
}
if (object is Set && object is! SplayTreeSet) {
// this is needed to have a consistent iteration order so that the produced
// hash is consistent independently of the Set insertion order
object = SplayTreeSet<dynamic>.from(object);
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We just care about the sort order and SplayTreeSet requires objects to be Comparable so this PR simplifies things by just sorting the set similar to the Map implementation.

if (object is Set) {
object = object.sorted(((dynamic a, dynamic b) => a.hashCode - b.hashCode));
}
if (object is Iterable) {
for (final value in object) {
Expand Down
11 changes: 11 additions & 0 deletions test/equatable_test.dart
Expand Up @@ -1000,6 +1000,17 @@ void main() {
expect(instanceA != instanceB, true);
expect(instanceA.hashCode != instanceB.hashCode, true);
});

test('should support non-comparable types', () {
final instanceA = SimpleEquatable<Set<Object>>(
Set.from(<Object>[Object()]),
);
final instanceB = SimpleEquatable<Set<Object>>(
Set.from(<Object>[Object()]),
);
expect(instanceA == instanceB, false);
expect(instanceA.hashCode == instanceB.hashCode, false);
});
});
});

Expand Down