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

Enforce Required, Named Parameters #42438

Closed
felangel opened this issue Jun 22, 2020 · 2 comments
Closed

Enforce Required, Named Parameters #42438

felangel opened this issue Jun 22, 2020 · 2 comments
Labels
area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. type-bug Incorrect behavior (everything from a crash to more subtle misbehavior)

Comments

@felangel
Copy link

felangel commented Jun 22, 2020

As a developer, when I define a class with a required, named parameter I would like all subclasses to have errors (or at least a warning) if the required parameter was not passed to the superclass.

class A {
  const A(this.x);

  final int x;
}

class B extends A {}

The above snippet currently won't compile (expected):
Screen Shot 2020-06-22 at 12 35 08 PM

If I refactor class A to make x a required, named parameter, however, the code compiles just fine without any error/warning (not expected).

import 'package:meta/meta.dart';

class A {
  const A({@required this.x});

  final int x;
}

class B extends A {}

Screen Shot 2020-06-22 at 12 36 58 PM

This will definitely not be an issue once NNBD lands in stable but for now it's seems there isn't a good way to safely support required, named parameters.

Additional Info

This tracker is for issues related to:

  • Analyzer
  • Dart core libraries ("meta")

Dart Version

Dart VM version: 2.8.4 (stable) (Wed Jun 3 12:26:04 2020 +0200) on "macos_x64"

OS

MacOSX

@keertip keertip added the area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. label Jun 23, 2020
@lrhn lrhn added the type-bug Incorrect behavior (everything from a crash to more subtle misbehavior) label Jun 24, 2020
@lrhn
Copy link
Member

lrhn commented Jun 24, 2020

The analyzer does indeed fail to recognize that the implicit super() of the default constructors (or a user-written generative constructor with an omitted super-call) is not passing a @required parameter.

Compiling should work because Dart compilers mostly ignore metadata like @required. Only the Dart analyzer understand the meaning of @required, but it too fails to give a warning here, (dartanalyzer version 2.9.0-18.0.dev).

@srawlins
Copy link
Member

This works now with the null-safety feature, required. No further work will be done to support the @required annotation.

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. type-bug Incorrect behavior (everything from a crash to more subtle misbehavior)
Projects
None yet
Development

No branches or pull requests

4 participants