Skip to content

'is' does not cast variables out of the scope #1493

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
nosmirck opened this issue Mar 4, 2021 · 1 comment
Closed

'is' does not cast variables out of the scope #1493

nosmirck opened this issue Mar 4, 2021 · 1 comment

Comments

@nosmirck
Copy link

nosmirck commented Mar 4, 2021

When using is casting should work with variables out of the scope too. Here is an example:

final A b = B(42);

void main() {
  //This one doesn't allow compilation because
  //"The getter 'i' isn't defined for the type A" even though
  //the variable has been casted inside the `if (b is B)`
  if (b is B) {
    print(b.i);
  }

  //Making a local scope reference of this variable works as expected.
  final _b = b;
  if (_b is B) {
    print(_b.i);
  }
}

abstract class A {}

class B extends A {
  final int i;
  B(this.i);
}

Tested this on the latest DartPad with null safety enabled.

@srawlins srawlins transferred this issue from dart-lang/sdk Mar 4, 2021
@leafpetersen
Copy link
Member

This is a general instance of this issue. We could promote final global variables (as opposed to fields), but only at the expense of breaking the symmetry between variables and getter/setter pairs. See here for a list of issues discussing possible approaches to making this easier to work with.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants