You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/language/enumerations.md
+59-51Lines changed: 59 additions & 51 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,68 +3,76 @@ title: Enumerations
3
3
sidebar_position: 13
4
4
---
5
5
6
-
Enumerations are sets of symbolic names bound to unique, constant values,
7
-
which can be compared by identity.
6
+
Enumerations are sets of symbolic names bound to unique, constant values, which can be compared by identity.
8
7
9
-
## Enum Declaration
8
+
## Enum declaration
10
9
11
-
Enums are declared using the `enum` keyword,
12
-
followed by the name of the enum, the raw type after a colon,
13
-
and the requirements, which must be enclosed in opening and closing braces.
10
+
Enums are declared using the `enum` keyword, followed by the name of the enum, the raw type after a colon, and the requirements, which must be enclosed in opening and closing braces.
14
11
15
-
The raw type must be an integer subtype, e.g. `UInt8` or `Int128`.
12
+
The raw type must be an integer subtype (e.g.,`UInt8` or `Int128`).
16
13
17
-
Enum cases are declared using the `case` keyword,
18
-
followed by the name of the enum case.
14
+
Enum cases are declared using the `case` keyword, followed by the name of the enum case.
19
15
20
-
Enum cases must be unique.
21
-
Each enum case has a raw value, the index of the case in all cases.
16
+
Enum cases must be unique. Each enum case has a raw value, which is the index of the case among all cases.
22
17
23
18
The raw value of an enum case can be accessed through the `rawValue` field.
24
19
25
-
The enum cases can be accessed by using the name as a field on the enum,
26
-
or by using the enum constructor,
27
-
which requires providing the raw value as an argument.
28
-
The enum constructor returns the enum case with the given raw value,
29
-
if any, or `nil` if no such case exists.
20
+
The enum cases can be accessed by using the name as a field on the enum or by using the enum constructor, which requires providing the raw value as an argument. The enum constructor returns the enum case with the given raw value, if any, or `nil` if no such case exists.
30
21
31
22
Enum cases can be compared using the equality operators `==` and `!=`.
32
23
33
-
```cadence
34
-
// Declare an enum named `Color` which has the raw value type `UInt8`,
35
-
// and declare three enum cases: `red`, `green`, and `blue`
36
-
//
37
-
access(all)
38
-
enum Color: UInt8 {
24
+
## Working with an enum declaration
39
25
40
-
access(all)
41
-
case red
26
+
1. Declare an enum named `Color`, which has the raw value type `UInt8`, and declare three enum cases (`red`, `green`, and `blue`):
42
27
43
-
access(all)
44
-
case green
28
+
```cadence
29
+
access(all)
30
+
enum Color: UInt8 {
31
+
32
+
access(all)
33
+
case red
34
+
35
+
access(all)
36
+
case green
45
37
46
-
access(all)
47
-
case blue
48
-
}
49
-
// Declare a variable which has the enum type `Color` and initialize
50
-
// it to the enum case `blue` of the enum
51
-
let blue: Color = Color.blue
52
-
// Get the raw value of the enum case `blue`.
53
-
// As it is the third case, so it has index 2
54
-
//
55
-
blue.rawValue // is `2`
56
-
// Get the `green` enum case of the enum `Color` by using the enum
57
-
// constructor and providing the raw value of the enum case `green`, 1,
58
-
// as the enum case `green` is the second case, so it has index 1
59
-
//
60
-
let green: Color? = Color(rawValue: 1) // is `Color.green`
61
-
// Get the enum case of the enum `Color` that has the raw value 5.
62
-
// As there are only three cases, the maximum raw value / index is 2.
63
-
//
64
-
let nothing = Color(rawValue: 5) // is `nil`
65
-
// Enum cases can be compared
66
-
Color.red == Color.red // is `true`
67
-
Color(rawValue: 1) == Color.green // is `true`
68
-
// Different enum cases are not the same
69
-
Color.red != Color.blue // is `true`
70
-
```
38
+
access(all)
39
+
case blue
40
+
}
41
+
```
42
+
43
+
1. Declare a variable that has the enum type `Color` and initialize it to the enum case `blue` of the enum:
44
+
45
+
```cadence
46
+
let blue: Color = Color.blue
47
+
```
48
+
49
+
1. Get the raw value of the enum case `blue`. Since it is the third case, it has an index of 2:
50
+
51
+
```cadence
52
+
blue.rawValue // is `2`
53
+
```
54
+
55
+
1. Get the `green` enum case of the enum `Color` by using the enum constructor and providing the raw value of the enum case `green`, 1. Since the enum case `green` is the second case, it has an index of 1:
56
+
57
+
```cadence
58
+
let green: Color? = Color(rawValue: 1) // is `Color.green`
59
+
```
60
+
61
+
1. Get the enum case of the enum `Color` with the raw value 5. As there are only three cases, the maximum raw value/index is 2:
0 commit comments