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

Better message for override inside extension type #59855

Open
FMorschel opened this issue Jan 7, 2025 · 5 comments
Open

Better message for override inside extension type #59855

FMorschel opened this issue Jan 7, 2025 · 5 comments
Labels
area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion.

Comments

@FMorschel
Copy link
Contributor

While testing a code for #59820 as bellow:

class C {
  C._(this.x);
  final int x;
}

extension type E(C c) implements C {
  @override
  int get x => 0;
}

On dartpad set to main, I get:

The getter doesn't override an inherited getter.

I don't think that is true considering implements C. Can anyone confirm this?

@FMorschel FMorschel added the area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. label Jan 7, 2025
@eernstg
Copy link
Member

eernstg commented Jan 7, 2025

An extension type can't override anything, it can only redeclare a member.

The difference is that when a member has been overridden, an invocation will always call the most specific implementation of that member, no matter whether that most specific implementation is known from the static type or not. An extension type member is only invoked when it is the statically known declaration for that member name. For example:

import 'package:meta/meta.dart';

class C {
  C._(this.x);
  final int x;
}

extension type E(C c) implements C {
  @redeclare
  int get x => 0;
}

The @redeclare metadata annotation indicates that there is a member of some superinterface (here: C) that also has the name x (and it is implied that the name clash was intended).

A redeclaration is much more flexible than an overriding declaration: The redeclaration can be a getter when the redeclared declaration is a method or vice versa (we can't do that in an overriding declaration), and the signatures don't have to be related.

The reason why there are no constraints is that the redeclaration is never invoked in a situation where it isn't known statically. In other words, if you're calling a redeclaration then a "jump to declaration" operation in an IDE will take you to the exact piece of code that will run at run time, there will never be a "more specific" redeclaration which will "override" it.

So you should simply use @redeclare rather than @override when the metadata sits on an extension type declaration.

@FMorschel
Copy link
Contributor Author

Ohh, I see. Thanks for the explanation!

@FMorschel
Copy link
Contributor Author

@eernstg would it be reasonable to reopen this as a request for a better message for @override inside extension types ? Maybe even mentioning @redeclare.

@eernstg
Copy link
Member

eernstg commented Jan 7, 2025

Sounds like a good idea! @bwilkerson, WDYT?

@bwilkerson
Copy link
Member

I agree, a better message would be helpful to users.

@bwilkerson bwilkerson reopened this Jan 7, 2025
@FMorschel FMorschel changed the title Not-inherited getter for extension type Better message for override inside extension type Jan 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion.
Projects
None yet
Development

No branches or pull requests

3 participants