enum EnumNamedParameters {
Is this okay? I couldn't find information on this, but when we have a declaring constructor (body or header) for enums, is it implicitly const? Or must we explicitly declare it as such?
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. |
Code-Review | +1 |
- Declaring constructors can have initializing parameters.
I think the phrase 'initializing parameters' is ambiguous. Perhaps it means 'declaring parameters', or perhaps 'initializing formals'? 'Declaring parameters' seems to be the one that isn't mentioned elsewhere.
OK, 'initializing formals' is a rather funny phrase to use as the standard phrase to categorize `this.x`, but that's what we have and we should rename it consistently if anybody comes up with a better one. ;-)
// as initializing formals and super parameters, just like other constructors in
I don't see any super parameters here. There are some in tests/language/declaring_constructors/syntax/body_syntax_test.dart so it should be fine to just avoid talking about them here.
this(final int x) : super(x + 1);
Interesting! This introduces a second instance variable whose name is `x`, and its getter will override the getter that reads the instance variable `x` in the superclass. It should work! But it would probably be helpful for future readers to add a comment explaining that we're doing this "waste some space in the superclass layout" trick. ;-)
this(final int x) : y = x, assert(x > 0), super(x + 1);
`final` could be omitted here and the new instance variable could have a name different from `y`, just so the more typical setup (with no instance variable shadowing) is tested as well.
this(final int x) : assert(x > 0);
Nit: Remove one space before `:`.
class C3(final int x) extends C1 {
Might as well make this parameter non-declaring, because that's much more likely in real code than the instance variable shadowing.
class C3(final int x) extends C1 {
`C4`. `x` could be non-declaring again.
// non-constant default values.
Perhaps add a few words in this comment to say that this is the header case, and conversely in tests/language/declaring_constructors/body/optional_parameter_nonconstant_default_error_test.dart that it's the body case.
enum EnumNamedParameters {
Is this okay? I couldn't find information on this, but when we have a declaring constructor (body or header) for enums, is it implicitly const? Or must we explicitly declare it as such?
A header constructor in an `enum` declaration is implicitly constant, https://github.com/dart-lang/language/blob/3911ceae94875924786c8548f94abfd3743b9080/accepted/future-releases/primary-constructors/feature-specification.md?plain=1#L957. But in the body it is required to be marked `const`, just like all other `enum` constructors in the body.
I'm afraid the current wording in the feature specification allows for the interpretation that the `const` on the declaring body constructor in an `enum` declaration can be omitted, too, but I'll fix that. ;-)
this({final int x = 1, required var int y});
So this one needs `const`, or an error should be expected.
e(1, 2);
Perhaps add a couple of other values passing one and zero actual arguments, to confirm that those parameters are actually treated as optional.
// Initializing parameters.
'A declaring parameter and an initializing formal' would be clearer. Or perhaps just drop `x` entirely.
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. |