Skip to content

Commit 2f07e4e

Browse files
committed
reimplement MapEntry as a record
keep it backwards compatible by preserving member names and implementing `Object` introduce `asPair` to allow direct usage as a record and `fromPair` to allow direct construction from a record
1 parent 19fce85 commit 2f07e4e

File tree

1 file changed

+4
-17
lines changed

1 file changed

+4
-17
lines changed

sdk/lib/core/map.dart

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -478,21 +478,15 @@ abstract interface class Map<K, V> {
478478
/// ]);
479479
/// print(map); // {1: A, 2: B, 3: C, 4: D}
480480
/// ```
481-
///
482-
/// Do not extend or implement the `MapEntry` class.
483-
/// If the Dart language introduces value types,
484-
/// the `MapEntry` class will be changed to such a type,
485-
/// and will likely no longer be able to be implemented or extended
486-
/// by classes.
487-
final class MapEntry<K, V> {
481+
extension type const MapEntry<K, V>.fromPair((K, V) asPair) implements Object {
488482
/// The key of the entry.
489483
///
490484
/// ```dart
491485
/// final map = {'theKey': 'theValue'}; // Map<String, String>
492486
/// var entry = map.entries.first; // MapEntry<String, String>
493487
/// print(entry.key); // 'theKey'
494488
/// ```
495-
final K key;
489+
K get key => asPair.$1;
496490

497491
/// The value associated to [key] in a map.
498492
///
@@ -501,15 +495,8 @@ final class MapEntry<K, V> {
501495
/// var entry = map.entries.first; // MapEntry<String, String>
502496
/// print(entry.value); // 'theValue'
503497
/// ```
504-
final V value;
498+
V get value => asPair.$2;
505499

506500
/// Creates an entry with [key] and [value].
507-
const factory MapEntry(K key, V value) = MapEntry<K, V>._;
508-
509-
const MapEntry._(this.key, this.value);
510-
511-
/// String representation intended for debugging only.
512-
///
513-
/// Not guaranteed to be stable over time.
514-
String toString() => "MapEntry($key: $value)";
501+
const MapEntry(K key, V value): this.fromPair((key, value));
515502
}

0 commit comments

Comments
 (0)