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

Type test operator is works unintended for class final members #35181

Closed
kikuchy opened this issue Nov 15, 2018 · 4 comments
Closed

Type test operator is works unintended for class final members #35181

kikuchy opened this issue Nov 15, 2018 · 4 comments
Labels
closed-as-intended Closed as the reported issue is expected behavior

Comments

@kikuchy
Copy link

kikuchy commented Nov 15, 2018

Problem

According to Language Tour, the combination of if statement and is operator can omit omit type cast by as operator in if statement.

But final ized class members cannot cast automatically.

class A {
  final Object a;
  A(this.a);
  void foo() {
    if (a is String) {
      print(a.codeUnits);    // Error: The getter 'codeUnits' isn't defined for the class 'dart.core::Object'.
    }
  }
}

final ized class members will not be exchanged for another instance, so its type will not change.

I think it should be compiled successfully.

Environment

  • Dart SDK Version (dart --version)
    • Dart 2.1.0-dev.5.0.flutter-a2eb050044
  • Whether you are using Windows, MacOSX, or Linux (if applicable)
    • MacOSX
@eernstg eernstg added the closed-as-intended Closed as the reported issue is expected behavior label Nov 15, 2018
@eernstg
Copy link
Member

eernstg commented Nov 15, 2018

This is working as intended. It would be sound to extend the ability to promote a variable to the case where it is a final variable of any kind, but it is actually only supported for local variables (which includes formal parameters of an enclosing function). So it's definitely a useful enhancement (and we have proposals for enhancing promotion in various ways, but it will take a while before it's part of the language), but with the current rules there is no promotion of a in your example.

@eernstg eernstg closed this as completed Nov 15, 2018
@eernstg
Copy link
Member

eernstg commented Nov 15, 2018

Here's the place where you can see that promotion only occurs for local variables.

More information about the history here: #25565.

@kikuchy
Copy link
Author

kikuchy commented Nov 15, 2018

we have proposals for enhancing promotion in various ways, but it will take a while before it's part of the language

On, I see. I'm looking forward to that proposals becomes part of the language.
Thank you for answering.

@eernstg
Copy link
Member

eernstg commented Nov 15, 2018

I'm looking forward to that proposals becomes part of the language.

Me too. ;-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-as-intended Closed as the reported issue is expected behavior
Projects
None yet
Development

No branches or pull requests

2 participants