Skip to content

Commit

Permalink
Add error about factory constructor generating an enum value (#3461)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
eernstg authored Nov 15, 2023
1 parent aab3033 commit 0593087
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions accepted/2.17/enhanced-enums/feature-specification.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,11 @@ It is a **compile-time error** to refer to a declared or default generative cons
* As the target of a redirecting generative constructor of the same `enum` declaration (`: this(...);`/`: this.targetName(...);`), or
* Implicitly in the enum value declarations of the same `enum` (`enumValueName(args)`/`enumValueName.targetName(args)`).

_No-one is allowed to invoke a generative constructor and create an instance of the `enum` other than the enumerated enum values.
That also means that a redirecting *factory* constructor cannot redirect to a generative constructor of an `enum`,
and therefore no factory constructor of an `enum` declaration can be `const`, because a `const` factory constructor must redirect to a generative constructor._
_No-one is allowed to invoke a generative constructor and create an instance of the `enum` other than the enumerated enum values._

It is a **compile-time error** if the constructor implicitly invoked by an enumerated enum value declaration is a factory constructor.

_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._

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._

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

0 comments on commit 0593087

Please sign in to comment.