Skip to content
This repository has been archived by the owner on Oct 22, 2024. It is now read-only.

Update code to Dart 3.0 #284

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

* Require Dart 3.0.
* Mark mixin classes as `mixin class`.
* Added a `Map.pairs` extension method to make it easier to work with maps using
Dart 3 record types.

## 1.17.2
Copy link
Contributor

Choose a reason for hiding this comment

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

This version was never published, so we can probably just drop the changelog entry.


Expand Down
1 change: 1 addition & 0 deletions lib/collection.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export 'src/functions.dart';
export 'src/iterable_extensions.dart';
export 'src/iterable_zip.dart';
export 'src/list_extensions.dart';
export 'src/map_extensions.dart';
export 'src/priority_queue.dart';
export 'src/queue_list.dart';
export 'src/union_set.dart';
Expand Down
1 change: 1 addition & 0 deletions lib/src/functions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import 'dart:collection';
import 'dart:math' as math;

import 'map_extensions.dart';
import 'utils.dart';

/// Creates a new map from [map] with new keys and values.
Expand Down
8 changes: 8 additions & 0 deletions lib/src/map_extensions.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

extension MapExtensions<K, V> on Map<K, V> {
/// Like [Map.entries], but returns each entry as a record.
Iterable<(K, V)> get pairs => entries.map((e) => (e.key, e.value));
Copy link
Contributor

Choose a reason for hiding this comment

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

Interesting...but why? What's the upside here? What do users get?

We're already going to allocate (however short-lived) MapEnty instances.

Copy link
Contributor

Choose a reason for hiding this comment

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

The main benefit is being able to loop over pairs with variables named something other than "key" and "value".

#289 (comment)

Copy link
Contributor

Choose a reason for hiding this comment

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

I think we're still hoping to make MapEntry an inline class over a (K, V), and if we are able to do that this .pairs extension will be useless. I think it's worth having it early in this package.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I doubt it will be over (K, V). More likely it's ({K key, V value}), since that will allow dynamic-typed code to keep working.

}
17 changes: 17 additions & 0 deletions test/extensions_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1900,6 +1900,23 @@ void main() {
});
});
});

group('Map', () {
group('pairs', () {
test('empty map', () {
expect(<int, int>{}.pairs, isEmpty);
});

test('single pair', () {
expect(<int, int>{1: 2}.pairs, equals([(1, 2)]));
});

test('multiple pairs', () {
expect(<int, int>{1: 2, 3: 4, 5: 6}.pairs,
equals([(1, 2), (3, 4), (5, 6)]));
});
});
});
}

/// Creates a plain iterable not implementing any other class.
Expand Down