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 propagation for final fields #43334

Closed
renggli opened this issue Sep 4, 2020 · 1 comment
Closed

Type propagation for final fields #43334

renggli opened this issue Sep 4, 2020 · 1 comment

Comments

@renggli
Copy link

renggli commented Sep 4, 2020

I think the type system is too conservative for final fields:

class Foo {
  final Object object;

  Foo(this.object);

  void bar() {
    if (object is String) {
      print(object.length);  // The getter 'length' isn't defined for the type 'Object'. 
    }
  }
}

Similarly for null type promotion:

class Bar {
  final String? message;

  Bar([this.message]);

  void bar() {
    if (message != null) {
      print(message.length);  // An expression whose value can be 'null' must be null-checked before it can be dereferenced.
    }
  }
}

Having to copy to a local variable before testing and accessing is cumbersome.

$ dart --version
Dart SDK version: 2.10.0-92.0.dev (dev) (Wed Sep 2 22:28:32 2020 -0700) on "macos_x64"
@mraleph
Copy link
Member

mraleph commented Sep 4, 2020

Final fields can still be overriden by a getter making any sort of static promotion without checks unsound.

class Bar extends Foo {
  @override
  Object get object => 
    Random.nextBool() ? 'today I am a string, some day I am ' : 42 ;
}

There are multiple threads going on about potential ways to tackle this on the language repo:

@mraleph mraleph closed this as completed Sep 4, 2020
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