File: enums.md

package info (click to toggle)
kotlin 1.3.31%2Bds1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 109,908 kB
  • sloc: java: 454,756; xml: 18,599; javascript: 10,452; sh: 513; python: 97; makefile: 69; ansic: 4
file content (64 lines) | stat: -rw-r--r-- 2,252 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# Enums

Goals:
* Better syntax for passing constructor parameters when defining enum constants
* Resolve issues with annotation syntax for enum constants

## Example

Simple enum:
``` kotlin
enum class Foo {
    A
    B
    C {
        override fun foo() { ... }
    }
  
    open fun foo() {}
}
```

Enum with constructor:
``` kotlin
enum class Foo(val s: String) {
    A("a")
    B("b")
    C("c") {
        override fun foo() { ... }
    }
  
    open fun foo() {}
}
```

Issues
* Enum literals syntax clash with annotation syntax
    * 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 entries 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 **chosen**: Leave unsupported, use cases are very few, and Java does not support it
    * Option 2.2: `A("s"): OtherType`

Example for option 1.4:

``` kotlin
enum class Foo(val s: String) {
    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, even if no member follows
  
    open fun foo() {}
}
```

Notes:
* 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