Skip to content
This repository has been archived by the owner on Feb 22, 2018. It is now read-only.

overriding a final property with a getter generates a warning #421

Closed
Andersmholmgren opened this issue Jan 22, 2016 · 4 comments
Closed

Comments

@Andersmholmgren
Copy link

In the following

class A {
  final String foo;
  A(this.foo);
}

class B extends A {
  B(String foo) : super(foo);

  String get foo => 'foo';
}

String get foo => 'foo'; generates a warning.

It doesn't complain if B implements A.

I've done this for a while and never had any problems. It would seem odd that this would be dissallowed in the language

@jmesserly
Copy link
Contributor

Hey, thanks for the feedback. This is a duplicate of #52 and dart-lang/sdk#24928. See those issues for more background. 24928 is still open--we have not settled this question yet.

It is a hard one, because supporting this causes widespread readability cost (and possibly performance cost), even in places where the feature is not used. For ahead-of-time compilation it's very helpful to know what is a field vs what is a virtual property.

That said we could make it easier to declare a field @virtual or something along those lines.

@jmesserly
Copy link
Contributor

BTW, the other reason for the warning: your instances of "B" will have a dummy storage slot that is never used. Sometimes folks don't realize that their objects will be bigger and have unused storage space.

@Andersmholmgren
Copy link
Author

Actually in the cases I found I could simply change it to implements (which I should have done in the first place).

I think the @virtual suggestion might be a good tradeoff

@Andersmholmgren
Copy link
Author

Actually I take that back. One of my cases did need to extend and strikes me as a legit case

It makes use of the parent classes with a fallback implementation for when the field is null

  Option<CompletedAction<E>> get completedAction =>
      super.completedAction.orElse(() => children
          .map((c) => c.completedAction)
          .firstWhere((ca) => ca is Some, orElse: () => const None()));

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Development

No branches or pull requests

2 participants