Discussion:
[dart-misc] Dart 2 Breaking Change: Removing defaultValue from Stream.{firstWhere, lastWhere}
'Leaf Petersen' via Dart Misc
2018-03-01 01:13:01 UTC
Permalink
*What is changing?*

As part of the migration of the Dart core libraries to Dart 2, we are
changing the signatures of `Stream.firstWhere` and `Stream.lastWhere` to be
more usable in a typed fashion.

We have already landed changes moving the signature of `firstWhere` from

Future firstWhere(bool test(T element), {dynamic defaultValue()}) {

to

Future<T> firstWhere(bool test(T element), {dynamic defaultValue(), T
orElse()}) {

and similarly for `lastWhere`. This was done to allow incremental
migration: `orElse` is intended to replace `defaultValue`.

As the last step of this process, we are now deprecating and will soon be
removing the `defaultValue` parameter to both methods.

Future<T> firstWhere(bool test(T element), {T orElse()}) {

*What will break?*

If you have code which calls `Stream.firstWhere` or `Stream.lastWhere` or
the equivalent methods on classes implementing `Stream` from
`package:async`, *and* you pass a defaultValue argument, then you will soon
begin receiving deprecation warnings, and soon after your code will break.

*How do I fix it?*

Anywhere you currently pass `defaultValue: <someFunction>` to one of these
methods, you should change your code to pass `orElse: <someFunction>`.

You can (and should!) do this now, assuming that you are on an up to date
version of flutter, or are using a Dart SDK later than 2.0.0-dev.23.0 .
This rollout was staged so that you can fix your code now and never be in a
broken state.

It is possible (though fairly unlikely) that changing your code to use
`orElse` will cause new analyzer warnings, since inference will now be
doing a better job of propagating types. You can always get back to the
original inference state by changing code that looks like:

```
s.firstWhere(f, defaultValue: e);
```

into


```
dynamic defaultValueFn() => e
s.firstWhere(f, orElse: defaultValueFn);
```

*Why is this change being made?*

Making `orElse` strongly typed allows inference to work better with these
APIs, making it more likely that the analyzer can help catch errors, and
making it less likely that your code will fail Dart 2 runtime checks.

I will update here when this change lands. Please reach out to me here or
offline with any concerns, and/or with help resolving any issues after this
change has landed.

thanks,
-leaf
--
For other discussions, see https://groups.google.com/a/dartlang.org/

For HOWTO questions, visit http://stackoverflow.com/tags/dart

To file a bug report or feature request, go to http://www.dartbug.com/new
---
You received this message because you are subscribed to the Google Groups "Dart Misc" group.
To unsubscribe from this group and stop receiving emails from it, send an email to misc+***@dartlang.org.
'Leaf Petersen' via Dart Misc
2018-03-01 05:29:35 UTC
Permalink
Correction: If you want to/need to change your code in a way that prevents
inference from kicking in (you probably don't), change

```
s.firstWhere(f, defaultValue: e);
```

into


```
T defaultValueFn() => (e as T)
s.firstWhere(f, orElse: defaultValueFn);
```

or

```
s.firstWhere(f, orElse: () => (e as T));
```

where `s` has type `Stream<T>`.

-leaf
Post by 'Leaf Petersen' via Dart Misc
*What is changing?*
As part of the migration of the Dart core libraries to Dart 2, we are
changing the signatures of `Stream.firstWhere` and `Stream.lastWhere` to be
more usable in a typed fashion.
We have already landed changes moving the signature of `firstWhere` from
Future firstWhere(bool test(T element), {dynamic defaultValue()}) {
to
Future<T> firstWhere(bool test(T element), {dynamic defaultValue(), T
orElse()}) {
and similarly for `lastWhere`. This was done to allow incremental
migration: `orElse` is intended to replace `defaultValue`.
As the last step of this process, we are now deprecating and will soon be
removing the `defaultValue` parameter to both methods.
Future<T> firstWhere(bool test(T element), {T orElse()}) {
*What will break?*
If you have code which calls `Stream.firstWhere` or `Stream.lastWhere` or
the equivalent methods on classes implementing `Stream` from
`package:async`, *and* you pass a defaultValue argument, then you will
soon begin receiving deprecation warnings, and soon after your code will
break.
*How do I fix it?*
Anywhere you currently pass `defaultValue: <someFunction>` to one of these
methods, you should change your code to pass `orElse: <someFunction>`.
You can (and should!) do this now, assuming that you are on an up to date
version of flutter, or are using a Dart SDK later than 2.0.0-dev.23.0 .
This rollout was staged so that you can fix your code now and never be in a
broken state.
It is possible (though fairly unlikely) that changing your code to use
`orElse` will cause new analyzer warnings, since inference will now be
doing a better job of propagating types. You can always get back to the
```
s.firstWhere(f, defaultValue: e);
```
into
```
dynamic defaultValueFn() => e
s.firstWhere(f, orElse: defaultValueFn);
```
*Why is this change being made?*
Making `orElse` strongly typed allows inference to work better with these
APIs, making it more likely that the analyzer can help catch errors, and
making it less likely that your code will fail Dart 2 runtime checks.
I will update here when this change lands. Please reach out to me here or
offline with any concerns, and/or with help resolving any issues after this
change has landed.
thanks,
-leaf
--
For other discussions, see https://groups.google.com/a/dartlang.org/

For HOWTO questions, visit http://stackoverflow.com/tags/dart

To file a bug report or feature request, go to http://www.dartbug.com/new
---
You received this message because you are subscribed to the Google Groups "Dart Misc" group.
To unsubscribe from this group and stop receiving emails from it, send an email to misc+***@dartlang.org.
'Leaf Petersen' via Dart Misc
2018-04-08 19:00:38 UTC
Permalink
This change has landed and was release in 2.0.0-dev.45.0 . As always,
please reach out if you need help resolving any issues with this.

thanks,
-leaf
Post by 'Leaf Petersen' via Dart Misc
*What is changing?*
As part of the migration of the Dart core libraries to Dart 2, we are
changing the signatures of `Stream.firstWhere` and `Stream.lastWhere` to be
more usable in a typed fashion.
We have already landed changes moving the signature of `firstWhere` from
Future firstWhere(bool test(T element), {dynamic defaultValue()}) {
to
Future<T> firstWhere(bool test(T element), {dynamic defaultValue(), T
orElse()}) {
and similarly for `lastWhere`. This was done to allow incremental
migration: `orElse` is intended to replace `defaultValue`.
As the last step of this process, we are now deprecating and will soon be
removing the `defaultValue` parameter to both methods.
Future<T> firstWhere(bool test(T element), {T orElse()}) {
*What will break?*
If you have code which calls `Stream.firstWhere` or `Stream.lastWhere` or
the equivalent methods on classes implementing `Stream` from
`package:async`, *and* you pass a defaultValue argument, then you will
soon begin receiving deprecation warnings, and soon after your code will
break.
*How do I fix it?*
Anywhere you currently pass `defaultValue: <someFunction>` to one of these
methods, you should change your code to pass `orElse: <someFunction>`.
You can (and should!) do this now, assuming that you are on an up to date
version of flutter, or are using a Dart SDK later than 2.0.0-dev.23.0 .
This rollout was staged so that you can fix your code now and never be in a
broken state.
It is possible (though fairly unlikely) that changing your code to use
`orElse` will cause new analyzer warnings, since inference will now be
doing a better job of propagating types. You can always get back to the
```
s.firstWhere(f, defaultValue: e);
```
into
```
dynamic defaultValueFn() => e
s.firstWhere(f, orElse: defaultValueFn);
```
*Why is this change being made?*
Making `orElse` strongly typed allows inference to work better with these
APIs, making it more likely that the analyzer can help catch errors, and
making it less likely that your code will fail Dart 2 runtime checks.
I will update here when this change lands. Please reach out to me here or
offline with any concerns, and/or with help resolving any issues after this
change has landed.
thanks,
-leaf
--
For more ways to connect visit https://www.dartlang.org/community
---
You received this message because you are subscribed to the Google Groups "Dart Misc" group.
To unsubscribe from this group and stop receiving emails from it, send an email to misc+***@dartlang.org.
To view this discussion on the web visit https://groups.google.com/a/dartlang.org/d/msgid/misc/5bb85796-bd9a-46af-8beb-6ea53d38bc5a%40dartlang.org.
Loading...