Skip to content

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

Closed
vsmenon opened this issue Dec 1, 2015 · 8 comments
Closed

Strong mode should provide a static cast #25076

vsmenon opened this issue Dec 1, 2015 · 8 comments
Labels
legacy-area-analyzer Use area-devexp instead. P2 A bug or feature request we're likely to work on type-enhancement A request for a change that isn't a bug

Comments

@vsmenon
Copy link
Member

vsmenon commented Dec 1, 2015

In many cases, users would like to quiet strong mode without an explicit "as" - especially when they are running in VM production mode.

@jmesserly
Copy link

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?

@jmesserly
Copy link

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.

@leafpetersen
Copy link
Member

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 ).

@jmesserly
Copy link

I feel like we already let more through than I like (e.g. #25368 ).

Ouch. That code hurts to look at. Excellent point.

@leafpetersen leafpetersen removed their assignment Feb 24, 2016
@kevmoo kevmoo added P2 A bug or feature request we're likely to work on type-enhancement A request for a change that isn't a bug and removed Priority-Medium labels Mar 1, 2016
@donny-dont
Copy link

@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?

@leafpetersen
Copy link
Member

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);
}

@donny-dont
Copy link

Is there an available package with it in there? Looks simple enough to add.

@jmesserly
Copy link

we did add declaration casts. also are in the process of investigating new strictness flags.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
legacy-area-analyzer Use area-devexp instead. P2 A bug or feature request we're likely to work on type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests

6 participants