-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
The "implicit-casts: false" behaviour changed between dart 2.1 and 2.2 #36233
Comments
fyi - @stereotype441 @leafpetersen @srawlins - was there an intended change around assignment casts? |
There was a regression in the interpretation of the implicit-casts flags in dev releases between 2.1 and 2.2 (I don't think it went out in a stable release, but could be wrong). If you want to allow assignment/declaration casts ( as in your example code above), use the following:
|
To be clear, the behavior that you are seeing now is correct: using only the |
Thanks @leafpetersen. |
It looks like it may already have gone away in 2.2. @stereotype441 can confirm. There was very little uptake on it, so I'm not surprised. The language is moving in the direction of removing implicit downcasts in future versions, since we get a great deal a lot of negative feedback about them. |
Thanks @leafpetersen! Just to make sure: is there an alternative downcast solution? Or do I need o use the |
You can use generic methods to get a hand-rolled version: // Note, order of type variables important
T cast<T extends S, S>(S from) => from as T;
void test() {
num y = 3;
int x = cast(y); // Inferred
String z = cast(y); // Error
} This isn't all that nice to use though. With the extension method proposal, you might be able to get nicer syntax (e.g. make cast an extension method so it can be used inline). Feel free to leave feedback on the main implicit cast issue here or on the sub-issue for discussion of a cast operator here. We haven't had a lot of external feedback one way or the other, and there has been some internal discussion of your use case, so some concrete input would be useful. |
We switched to Dart 2 as soon as it was available, and have used this
analysis_options.yaml
since then:We got a lot of warnings because of
implicit-casts: true
and fixed them by changing the offending lines to this:and because it was an assignment, it solved the
implicit-casts
issue.Now suddenly with the switch to Dart 2.2, this assignment downcast is not working anymore. There was nothing in the Changelog that explained why it isn't working anymore.
Am I right to assume, that now, the only option I have, is to use
as
to forcefully cast in cases like this?Is there another option, where I can have the previous behaviour, that allows me to only downcast, and warn if the types are incompatible?
The text was updated successfully, but these errors were encountered: