Skip to content

reimplement MapEntry as a record #61071

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
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 4 additions & 17 deletions sdk/lib/core/map.dart
Original file line number Diff line number Diff line change
Expand Up @@ -478,21 +478,15 @@ abstract interface class Map<K, V> {
/// ]);
/// print(map); // {1: A, 2: B, 3: C, 4: D}
/// ```
///
/// Do not extend or implement the `MapEntry` class.
/// If the Dart language introduces value types,
/// the `MapEntry` class will be changed to such a type,
/// and will likely no longer be able to be implemented or extended
/// by classes.
final class MapEntry<K, V> {
extension type const MapEntry<K, V>.fromPair((K, V) asPair) implements Object {
/// The key of the entry.
///
/// ```dart
/// final map = {'theKey': 'theValue'}; // Map<String, String>
/// var entry = map.entries.first; // MapEntry<String, String>
/// print(entry.key); // 'theKey'
/// ```
final K key;
K get key => asPair.$1;

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

/// Creates an entry with [key] and [value].
const factory MapEntry(K key, V value) = MapEntry<K, V>._;

const MapEntry._(this.key, this.value);

/// String representation intended for debugging only.
///
/// Not guaranteed to be stable over time.
String toString() => "MapEntry($key: $value)";
const MapEntry(K key, V value) : this.fromPair((key, value));
}