Skip to content

Commit 1adb9ec

Browse files
author
Jarvan Zhang
committed
override equals and toString for Pair
1 parent 858a194 commit 1adb9ec

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

lib/src/foundation/pair/pair.dart

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,19 @@ class Pair<F, S> {
2121
final S second;
2222

2323
const Pair(this.first, this.second);
24+
@override
25+
bool operator ==(Object other) =>
26+
identical(this, other) ||
27+
other is Pair<F, S> &&
28+
runtimeType == other.runtimeType &&
29+
first == other.first &&
30+
second == other.second;
31+
32+
@override
33+
int get hashCode => first.hashCode ^ second.hashCode;
34+
35+
@override
36+
String toString() => 'Pair($first, $second)';
2437
}
2538

2639
/// create pair for given elements first and second.

test/pair/pair_test.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,12 @@ void main() {
1010
expect("first", result.first);
1111
expect(1, result.second);
1212
});
13+
14+
test("pair equal", () {
15+
var pair1 = pairOf("first", 2);
16+
var pair2 = pairOf("first", 2);
17+
18+
assert(pair1 == pair2);
19+
});
1320
});
1421
}

0 commit comments

Comments
 (0)