sdk
sdk copied to clipboard
Should pkg/meta's @required be deprecated for Dart v3?
My understanding is that, similar to pkg/meta's @alwaysThrows, @required has no use in Dart v3:
// Dart v2 before null-safety
import 'package:meta/meta.dart';
void a({@required String name}) {}
// Dart v2 after null-safety
void a({required String name}) {}
Anyone, not sure... figured I'd highlight this for others to look at.
Somewhat related, but not quite: https://github.com/dart-lang/sdk/issues/49066
Deprecating it sounds reasonable to me. I can't think of any value it has in a null-safe world.
Do we currently give a warning if the annotation is used in null-safe code? If so, we could consider it effectively deprecated, and just remove it in a version of package:meta which requires SDK 3.0. (Which will have to be a major version increment of package:meta anyway).
We can also deprecate the annotation now, with a comment of "migrate to null safety!".
Do we currently give a warning if the annotation is used in null-safe code?
Yes, though we're marking it as an error. Given
import 'package:meta/meta.dart';
void f({@required int x}) {}
the analyzer will produce the following:
error: With null safety, use the 'required' keyword, not the '@required' annotation. (missing_default_value_for_parameter at [test_package] lib/test.dart:3)
The correction message suggests removing the @.
I suspect that's sufficient.
We can also deprecate the annotation now, with a comment of "migrate to null safety!".
If we want to do something like that we should probably just flag any use of the version override comment with too low a version number.
Do we want to consider outright removing it rather than just deprecating?
I'd release a version of package:meta with an SDK requirement of >=3.0.0-0 and remove required from it (along with alwaysThrows, maybe the other already deprecated annotations too).
Anyone moving to that package will have everything they need in Dart 3.0. Those still doing Dart <2.12 code must stay on the older package.
The required annotation is not wrong to use for Dart <2.12 code. Migrating away from using it will happen automatically when you move to Dart >=2.12/3.0.