Skip to content

Commit 0593087

Browse files
authored
Add error about factory constructor generating an enum value (#3461)
This PR introduces a compile-time error for the situation where the constructor which is implicitly invoked by an `<enumEntry>` is not generative. The reason why this error is added at this time is that an extension type that implements an `enum` type `E` is capable of declaring a constant constructor that allows the declaration `E` to have a constant factory constructor. That constructor will necessarily return one of the objects created by an `<enumEntry>` in the declaration of `E`, and this means that we could have multiple `<enumEntry>` constructs denoting the same object. That should never occur in an `enum` declaration, so we need this new error. See #3460 for further information and examples.
1 parent aab3033 commit 0593087

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

accepted/2.17/enhanced-enums/feature-specification.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,11 @@ It is a **compile-time error** to refer to a declared or default generative cons
6464
* As the target of a redirecting generative constructor of the same `enum` declaration (`: this(...);`/`: this.targetName(...);`), or
6565
* Implicitly in the enum value declarations of the same `enum` (`enumValueName(args)`/`enumValueName.targetName(args)`).
6666

67-
_No-one is allowed to invoke a generative constructor and create an instance of the `enum` other than the enumerated enum values.
68-
That also means that a redirecting *factory* constructor cannot redirect to a generative constructor of an `enum`,
69-
and therefore no factory constructor of an `enum` declaration can be `const`, because a `const` factory constructor must redirect to a generative constructor._
67+
_No-one is allowed to invoke a generative constructor and create an instance of the `enum` other than the enumerated enum values._
68+
69+
It is a **compile-time error** if the constructor implicitly invoked by an enumerated enum value declaration is a factory constructor.
70+
71+
_A redirecting factory constructor can redirect to a generative constructor of an `extension type`, which may in turn return one of the existing values of that `enum`. This must be an error, because the enumerated values of an `enum` declaration must be distinct._
7072

7173
It's a **compile-time error** if the enum declaration contains a static or instance member declaration with the name `values`, or if the superclass or any superinterface of the enum declaration has an interface member named `values`. _A `values` static constant member will be provided for the class, this restriction ensures that there is no conflict with that declaration._
7274

@@ -460,3 +462,5 @@ There is a chance that people will start using `enum` declarations to declare si
460462
1.7, 2022-02-16: Disallow overriding `operator==` and `hashCode` too.
461463
1.8, 2022-03-08: Make it explicit that an enum constructor cannot use the new super-parameters.
462464
1.9, 2023-01-13: Adjust the grammar to allow enum entry using `new` to specify a constructor.
465+
1.10, 2023-11-10: Add an error about factory constructors: They can not be used
466+
to create enum values.

0 commit comments

Comments
 (0)