Final decision about enum new syntax (specification)
This commit is contained in:
+9
-9
@@ -37,28 +37,28 @@ Issues
|
||||
* Option 1.1: Forbid short annotation syntax in enums. **downside**: cannot annotate functions/properties/classes in this enum
|
||||
* Option 1.2: Add a separator between enum constants and members, and forbid short annotation syntax only on enum entriesc themselves. **downside**: separator is not intuitive, hard to think of when doing this for the first time (the error message will be rather clear and instructive, though)
|
||||
* Option 1.3: prefix each entry with a soft-keyword, e.g. `entry`. **downside**: verbosity
|
||||
* Option 1.4 **chosen**: Add a semicolon separator after the last enum constant, **and** a comma separator between different enum constants.
|
||||
* How do we specify other supertypes for a constant (if any)
|
||||
* Option 2.1: Leave unsupported, use cases are very few, and Java does not support it
|
||||
* Option 2.1 **chosen**: Leave unsupported, use cases are very few, and Java does not support it
|
||||
* Option 2.2: `A("s"): OtherType`
|
||||
|
||||
Example for option 1.2:
|
||||
Example for option 1.4:
|
||||
|
||||
``` kotlin
|
||||
enum class Foo(val s: String) {
|
||||
A("a") // semicolon CAN NOT be used here!
|
||||
B("b")
|
||||
A("a"), // semicolon CAN NOT be used here!
|
||||
B("b"), // comma is MANDATORY after each enum constant except the last one
|
||||
C("c") {
|
||||
override fun foo() { ... }
|
||||
}; // semicolon is MANDATORY here, if a member follows
|
||||
}; // semicolon is MANDATORY here, even if no member follows
|
||||
|
||||
// if no semicolon was provided, `open` is another enum entry
|
||||
open fun foo() {}
|
||||
}
|
||||
```
|
||||
|
||||
Notes:
|
||||
* No overhead in the most common case of no members at all: `enum class E {A B C}
|
||||
* Clear error message: if the parser sees a member, but no semicolon before it:
|
||||
* it reports an error saying "There must be a semicolon separating enum entries from members", which is rather instructive
|
||||
* Almost no overhead in the most common case of no members at all: `enum class E {A, B, C; }
|
||||
* Clear error message: if the parser sees no semicolon after the last constant:
|
||||
* it reports an error saying "There must be a semicolon after the last enum entry", which is rather instructive
|
||||
* a quick fix can even guess the right position for the semicolon most of the time
|
||||
* Today, there's no way of naming an enum entry `public` (or any other soft-keyword used as a modifier), which is unfortunate
|
||||
|
||||
Reference in New Issue
Block a user