-
Notifications
You must be signed in to change notification settings - Fork 83
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
Provide faster implementation for whereNotNull #274
base: master
Are you sure you want to change the base?
Conversation
@override | ||
bool moveNext() { | ||
while (_source.moveNext()) { | ||
final el = _source.current; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use var
, not final
.
We don't generally use final
for locals.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
final
locals is getting enforcement internally and is very likely the direction we'll go with Dash team packages as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Still, keep the style here consistent until we do make a change. Don't mix styles.
} | ||
|
||
@override | ||
T get current => _current!; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use => _current ?? (throw StateError("no element"))
.
@@ -593,6 +594,42 @@ extension IterableExtension<T> on Iterable<T> { | |||
} | |||
} | |||
|
|||
/// An iterable which emits all non-`null` elements of the source | |||
/// iterable. | |||
class _NotNullIterable<T extends Object> extends IterableBase<T> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
extends Iterable<T>
.
if (element != null) yield element; | ||
} | ||
} | ||
Iterable<T> whereNotNull() => _NotNullIterable(this); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we do
var self = this;
if (self is Iterable<T>) return self;
return _NotNullIterable<T>(this);
}
Probably not worth it, but we don't have a good way to prevent you from using the method on Iterable<int>
.
(We could put a Never whereNotNull() => throw "Not needed!";
as extension <T extends Object> on Iterable<T>
, then you'd see if you get that instead.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a very important check, since it transform a new Iterable
instance (_NotNullIterable
) to just a casting.
Since the extension is resolved in the variable level, not in the instance:
void main() {
List<int?> l1 = <int>[1,2];
List<int> l2 = <int>[3,4];
List<int?> l3 = <int?>[1,2];
print(l1.isNullable);
print(l2.isNullable);
print(l3.isNullable);
}
extension _ItrExt<T extends Object> on Iterable<T> {
String get isNullable => "NO<T>";
}
extension _ItrNullableExt<T extends Object> on Iterable<T?> {
String get isNullable => this is Iterable<T> ? "NO<T?>" : "YES<T?>";
}
/////////////////
// OUTPUT:
/////////////////
// NO<T?>
// NO<T>
// YES<T?>
We now have We should optimize that if necessary, and make |
Replace
sync*
with handwritten implementation for now. We can undo the change ifsync*
becomes as fast as handwritten code.Closes #273