Skip to content
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

make 'as' not throw if the expression does not have the type. #5031

Closed
lrhn opened this issue Sep 10, 2012 · 4 comments
Closed

make 'as' not throw if the expression does not have the type. #5031

lrhn opened this issue Sep 10, 2012 · 4 comments
Labels
area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). closed-obsolete Closed as the reported issue is no longer relevant 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

@lrhn
Copy link
Member

lrhn commented Sep 10, 2012

The 'as' cast operator throws if its operand does not have the specified type.
That makes it useless without a prior type test (or implicit knowledge), but in that case, it doing an explicit test is redundant. I find myself not using it because I don't want to introduce an extra, unnecessary, test.

I suggest changing it to make it either simpler or useful on its own.
That is, either:

  1. Don't do a test in unchecked mode, but simply change the static type of the expression. I.e., make exp as Foo equivalent to assigning exp to a Foo variable and reading it back, but as an inline cast. In checked mode it would throw, just as assignment would. Or ...
  2. Let exp as Foo evaluate to null if the value of exp is not a Foo (as in C#). That is, return null where it currently throws. This makes 'as' useful on its own, without a prior type check. Instead of doing 'var tmp = exp; Foo foo = tmp is Foo ? tmp : null;' you just do 'Foo foo = exp as Foo;'.
@gbracha
Copy link
Contributor

gbracha commented Nov 6, 2012

Set owner to @gbracha.
Added Accepted label.

@kevmoo
Copy link
Member

kevmoo commented Apr 5, 2014

Has the ship sailed on this?

@gbracha
Copy link
Contributor

gbracha commented Apr 7, 2014

Yes.

@gbracha
Copy link
Contributor

gbracha commented Apr 7, 2014

Yes meaning, yes the ship has sailed. It won't change before Dart 2.0 if ever. It might be ok to change it then, since it is not really a breaking change (though any change is breaking in the sense that someone might be trying to catch the error etc.).

@lrhn lrhn added Type-Enhancement area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). labels Apr 7, 2014
@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 accepted labels Feb 29, 2016
@matanlurey matanlurey added the closed-obsolete Closed as the reported issue is no longer relevant label Jun 19, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). closed-obsolete Closed as the reported issue is no longer relevant 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

4 participants