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

Remove IterableExtensions that have exact equivalents in the SDK #331

Closed
wants to merge 3 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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
- Shuffle `IterableExtension.sample` results.
- Fix `mergeSort` when the runtime iterable generic is a subtype of the static
generic.
- Remove `firstOrNull`, `lastOrNull`, `singleOrNull` and `elementAtOrNull()`
Copy link
Contributor

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
Contributor 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
Contributor

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
Contributor

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
Contributor

@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.)

from `IterableExtensions`. Since Dart 3.0, exact equivalents to these are
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
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
Loading