-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Why are implicit downcasts allowed by default in strong mode? #30392
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
Comments
The linked discussion of Strong mode was originally an opt-in mode, and so we mostly aimed to minimize the amount of code changes required to get its benefits - hence the decision to keep implicit downcasts as the default. In retrospect, we may have underestimated people's willingness to change their code... or we may not have. :) In terms of Dart 2.0, there is not consensus on the Dart team that removing implicit casts would be a good change. Implicit casts definitely contribute to the concision of the language, albeit at no small expense to static analyzability. So currently void takesInt(int n) {}
void test() {
num n = 2.0;
// disallowed with --no-declaration-casts
// disallowed with --no-implicit-casts unless --declaration-casts is set
int i = n;
// disallowed with --no-implicit-casts
takesInt(n);
} |
Thanks for the info! I'm still a little confused though; are there examples of where this default is an advantage? I mentioned it to a few people today, and they all said "WTF?" or "Yeah but this is fixed in strong mode, right?". None of them expected this and would probably be frustrated if they hit a runtime error they would've expected to have been caught at dev time. I can't come up with any scenarios where I wouldn't expect to put an explicit cast in - I don't know why anybody would say "hey, I wish this just silently did potentially-dangerous downcasts for me". That's not to say I think there aren't any - but if there are, then maybe I can learn something new by understanding them :-) |
Slightly related: #30397 |
IMO, For v2 people are being told everything is strong, but I really can't help but think that more developers would expect this to be a compile error than not. Since the goal of v2 is to be stronger and breaking changes are already being made; it seems like a perfect opportunity to help improve peoples code and eliminate more potential runtime errors. |
By coincidence, I just got this runtime error:
I'd not called I don't expect to influence this decision; but I am really interested in others opinions since the only pros I can come up with (don't have to type a cast) are outweighed by the cons (intent/expectation not clear in the code; and possible runtime failures). Maybe I'm overlooking some pros :-) (Or would you accept a PR for |
FWIW, I don't agree this makes the language concise :) // No static error, but a common runtime one (in Dart 2.0).
List<T> listOfThing<T>(Iterable<T> iterable) => iterable; |
Make the code safer. Context: dart-lang/sdk#30392
I think I may have been bitten by this again! |
Make the code safer. Context: dart-lang/sdk#30392
Duplicate of #31410 |
In strong mode, this code gives no warnings but crashes at runtime:
I understand this is a deliberate decision and can be "fixed" by using
implicit-casts: false
but it seems like a crazy default to me for something claiming to be strong. I can't find any info on why this was decided - most language don't seem to allow this and it doesn't seem to be something that people often claim about.I think this may be discussed here; seems like it was related to the Flutter codebase:
I'd love this to be reconsidered, but at least, it'd be good to understand why it's this way. It seems like it's just gonna result in runtime errors that could easily be avoided. What's wrong with explicitly casting when required, or disabling this on your entire project if that's really a problem? It seems strange to weaken the type system because of one codebase (which would opt-out of this, or even insert explicit casts which would be an improvement IMO).
The text was updated successfully, but these errors were encountered: