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

Remove IterableExtensions that have exact equivalents in the SDK #331

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
## 1.19.0-wip
## 2.0.0-wip

- Adds `shuffled` to `IterableExtension`.
- Shuffle `IterableExtension.sample` results.
- Fix `mergeSort` when the runtime iterable generic is a subtype of the static
generic.
- Remove `firstOrNull`, `lastOrNull`, `singleOrNull` and `elementAtOrNull()`
from `IterableExtensions`. Since Dart 3.0, exact equivalents to these are
Copy link
Member

Choose a reason for hiding this comment

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

This will HAVE to be a breaking change (v2.0.0) since we are removing things.

Better to have a deprecation release first!

Copy link
Author

Choose a reason for hiding this comment

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

If these are deprecated, it is impossible to switch to the non-deprecated version. You can't switch from .firstOrNull to .firstOrNull.
A removal is fully seamless, on the other hand. Could you check the internal discussion that I CC'd you on

Copy link
Member

Choose a reason for hiding this comment

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

Oh! I guess as long as the SDK constraint is correct.

I think we'll still need a breaking change version, though. @natebosch ? @devoncarew ?

Copy link
Member

Choose a reason for hiding this comment

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

A removal is fully seamless, on the other hand

This is probably true for the vast majority of code. Code with import 'dart:core' as core; can have trouble here, but that's pretty rare outside of generated code, and most also have an unprefixed import 'dart:core';.

We could consider exporting the dart:core extension here. That should keep it non-breaking for even the edge cases. I think the analyzer will still have a diagnostic - but it might switch from unused import to "all used symbols from this import are also available from another import".

WDYT @lrhn

Copy link
Member

@lrhn lrhn Feb 1, 2024

Choose a reason for hiding this comment

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

I'd prefer to not export the extension from dart:core.
We can, but I don't think it'll ever bring us anything but headache.

And do it in a major version increment release, just to be technically allowed to be breaking, even if we hope that nobody notices.

It's not entirely non-breaking, even if we expose extensions with the same signatures, on types and name.
If someone uses show or hide of IterableExtension for a reason, or use explicit extension invocation, then it can break when that extension no longer has the members.
Unlikely, not impossible.

At least we dodge the bullet of having the extension names themeslves conflict, because this one is called IterableExtension and the one in dart:collection is called IterableExtensions.
So we could export it. But let's not take that kind of chances.
(And thank deity for lack of consistency.)

available in Dart core, so 'package:collection' would only be shadowing these.
- Require Dart `^3.1.0`
- Mark "mixin" classes as `mixin`.

Expand Down
39 changes: 0 additions & 39 deletions lib/src/iterable_extensions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -269,13 +269,6 @@ extension IterableExtension<T> on Iterable<T> {
return null;
}

/// The first element, or `null` if the iterable is empty.
T? get firstOrNull {
var iterator = this.iterator;
if (iterator.moveNext()) return iterator.current;
return null;
}

/// The last element satisfying [test], or `null` if there are none.
T? lastWhereOrNull(bool Function(T element) test) {
T? result;
Expand All @@ -297,12 +290,6 @@ extension IterableExtension<T> on Iterable<T> {
return result;
}

/// The last element, or `null` if the iterable is empty.
T? get lastOrNull {
if (isEmpty) return null;
return last;
}

/// The single element satisfying [test].
///
/// Returns `null` if there are either no elements
Expand Down Expand Up @@ -348,32 +335,6 @@ extension IterableExtension<T> on Iterable<T> {
return result;
}

/// The single element of the iterable, or `null`.
///
/// The value is `null` if the iterable is empty
/// or it contains more than one element.
T? get singleOrNull {
var iterator = this.iterator;
if (iterator.moveNext()) {
var result = iterator.current;
if (!iterator.moveNext()) {
return result;
}
}
return null;
}

/// The [index]th element, or `null` if there is no such element.
///
/// Returns the element at position [index] of this iterable,
/// just like [elementAt], if this iterable has such an element.
/// If this iterable does not have enough elements to have one with the given
/// [index], the `null` value is returned, unlike [elementAt] which throws
/// instead.
///
/// The [index] must not be negative.
T? elementAtOrNull(int index) => skip(index).firstOrNull;

/// Associates the elements in `this` by the value returned by [key].
///
/// Returns a map from keys computed by [key] to the last value for which
Expand Down
11 changes: 0 additions & 11 deletions lib/src/list_extensions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -259,17 +259,6 @@ extension ListExtensions<E> on List<E> {
return true;
}

/// The [index]th element, or `null` if there is no such element.
///
/// Returns the element at position [index] of this list,
/// just like [elementAt], if this list has such an element.
/// If this list does not have enough elements to have one with the given
/// [index], the `null` value is returned, unlike [elementAt] which throws
/// instead.
///
/// The [index] must not be negative.
E? elementAtOrNull(int index) => (index < length) ? this[index] : null;

/// Contiguous [slice]s of `this` with the given [length].
///
/// Each slice is a view of this list [length] elements long, except for the
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: collection
version: 1.19.0-wip
version: 2.0.0-wip
description: >-
Collections and utilities functions and classes related to collections.
repository: https://github.com/dart-lang/collection
Expand Down
62 changes: 0 additions & 62 deletions test/extensions_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -426,17 +426,6 @@ void main() {
0);
});
});
group('.firstOrNull', () {
test('empty', () {
expect(iterable([]).firstOrNull, null);
});
test('single', () {
expect(iterable([1]).firstOrNull, 1);
});
test('first of multiple', () {
expect(iterable([1, 3, 5]).firstOrNull, 1);
});
});
group('.lastWhereOrNull', () {
test('empty', () {
expect(iterable([]).lastWhereOrNull(unreachable), null);
Expand Down Expand Up @@ -474,17 +463,6 @@ void main() {
7);
});
});
group('.lastOrNull', () {
test('empty', () {
expect(iterable([]).lastOrNull, null);
});
test('single', () {
expect(iterable([1]).lastOrNull, 1);
});
test('last of multiple', () {
expect(iterable([1, 3, 5]).lastOrNull, 5);
});
});
group('.singleWhereOrNull', () {
test('empty', () {
expect(iterable([]).singleWhereOrNull(unreachable), null);
Expand Down Expand Up @@ -526,17 +504,6 @@ void main() {
null);
});
});
group('.singleOrNull', () {
test('empty', () {
expect(iterable([]).singleOrNull, null);
});
test('single', () {
expect(iterable([1]).singleOrNull, 1);
});
test('multiple', () {
expect(iterable([1, 3, 5]).singleOrNull, null);
});
});
group('.lastBy', () {
test('empty', () {
expect(iterable([]).lastBy((dynamic _) {}), {});
Expand Down Expand Up @@ -1204,21 +1171,6 @@ void main() {
});
});
});
group('.elementAtOrNull', () {
test('empty', () async {
expect(iterable([]).elementAtOrNull(0), isNull);
});
test('negative index', () async {
expect(() => iterable([1]).elementAtOrNull(-1),
throwsA(isA<RangeError>()));
});
test('index within range', () async {
expect(iterable([1]).elementAtOrNull(0), 1);
});
test('index too high', () async {
expect(iterable([1]).elementAtOrNull(1), isNull);
});
});
group('.slices', () {
test('empty', () {
expect(iterable(<int>[]).slices(1), []);
Expand Down Expand Up @@ -1820,20 +1772,6 @@ void main() {
['1', 'b']);
});
});
group('.elementAtOrNull', () {
test('empty', () async {
expect([].elementAtOrNull(0), isNull);
});
test('negative index', () async {
expect(() => [1].elementAtOrNull(-1), throwsA(isA<RangeError>()));
});
test('index within range', () async {
expect([1].elementAtOrNull(0), 1);
});
test('index too high', () async {
expect([1].elementAtOrNull(1), isNull);
});
});
group('.slices', () {
test('empty', () {
expect(<int>[].slices(1), []);
Expand Down