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

Setting of super.named parameter is recognized as unused or incorrect #59792

Closed
ChaserVasya opened this issue Dec 22, 2024 · 2 comments
Closed
Labels
area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion.

Comments

@ChaserVasya
Copy link

ChaserVasya commented Dec 22, 2024

Snippet

class A{
  final bool b;

  A({this.b = false});
}


class _B extends A{
  _B({super.b = true});
}

class _C extends A{
  _C(): super.b = true;
}

void fn(){
  _B(); 
  _C();
}

What I want

I want to avoid _B({b: true}) when I can set in constructor and to pass analyzer lints

Problem

B.b: is unused. (Okay. It looks like expected. But it has not hotfix by analyzer and I create hotfix as _C class)
C.b: error: 'b' isn't a field in the enclosing class.

Снимок экрана 2024-12-22 в 14 42 53

Version

Dart SDK version: 3.5.4 (stable) (Wed Oct 16 16:18:51 2024 +0000) on "macos_arm64"

@ChaserVasya ChaserVasya added the area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. label Dec 22, 2024
@ChaserVasya ChaserVasya changed the title Set of super.named parameter is recognized as unused Set of super.named parameter is recognized as unused or incorrect Dec 22, 2024
@ChaserVasya ChaserVasya changed the title Set of super.named parameter is recognized as unused or incorrect Setting of super.named parameter is recognized as unused or incorrect Dec 22, 2024
@incendial
Copy link
Contributor

incendial commented Dec 22, 2024

For C this should work as expected:

class C extends A {
  const C(): super(b: true);
}

@lrhn
Copy link
Member

lrhn commented Dec 22, 2024

The code is recognized as incorrect. It is incorrect, the syntax, super.id = e in an initializer list, is not allowed, and it's not even clear what it should mean.
It looks like it assigns a value to a superclass instance variable, but that's not allowed and likely never will be. It breaks abstraction to rely on a superclass implementing a getter using an instance variable. The subclass only less that the superclass has a getter and a constructor parameter, the rest is implementation details.

It can mean passive the expression's value to the super-constructor parameter of the same name, but that's not how you pass parameters (and even if it was made to work, it won't work with positional parameters).
If that's what you want, the syntax is _C(): super(b: true);.

This is a syntax error. It's not clear what the code is intended to do. That makes it hard to have a hotfix that fixes it.

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