-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Strong mode should provide a static cast #25076
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
One possibility: var base = new Base();
Derived d = base; // static cast In other words, treat that pattern as an explicit downcast. Only in VariableDeclarationStatement, when the type is explicitly mentioned on the left hand side. It would not affect how other implicit downcasts are reported. It was the Dart "cast" pattern before "as" existed. Would that work? Too special casey? |
Also, maybe it's too sneaky, even with the type explicitly mentioned on LHS. I guess the question with a "static_cast" function is where to put it. |
It's not clear that a static downcast is enough. There was definitely interest in a static downcast, but I think there are use cases where folks want a flat out cast. In general, I'm hesitant to make assignments even more forgiving - I feel like we already let more through than I like (e.g. #25368 ). |
Ouch. That code hurts to look at. Excellent point. |
@leafpetersen I definitely want a flat out cast ala a static_cast in C++. Is this the issue to follow or open a new one? |
This one works. In the short term you will likely have to pull in a package to get it. Or roll your own. This is, de-facto, a static cast in strong mode: // ignore: STRONG_MODE_DOWN_CAST_COMPOSITE
/*=To*/ downcast/*<From, To extends From>*/(From x) => x;
// ignore: STRONG_MODE_DOWN_CAST_COMPOSITE
/*=To*/ cast/*<To>*/(dynamic x) => x;
void test() {
dynamic d;
List<int> x = downcast/*<dynamic, List<int>>*/(d);
List<int> y = cast/*<List<int>>*/(d);
} |
Is there an available package with it in there? Looks simple enough to add. |
we did add declaration casts. also are in the process of investigating new strictness flags. |
In many cases, users would like to quiet strong mode without an explicit "as" - especially when they are running in VM production mode.
The text was updated successfully, but these errors were encountered: