Skip to content

Commit

Permalink
Add error about factory constructors generating enum values
Browse files Browse the repository at this point in the history
  • Loading branch information
eernstg committed Nov 10, 2023
1 parent aab3033 commit a1aff7c
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 a declaration of an enumerated value of an `enum` declaration gives rise to an invocation of 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 a1aff7c

Please sign in to comment.