Skip to content

Commit 7b71f65

Browse files
authored
Merge pull request #237 from onflow/references
Edit References and Enumerations articles
2 parents 9238096 + 9c9e19c commit 7b71f65

2 files changed

Lines changed: 172 additions & 204 deletions

File tree

docs/language/enumerations.md

Lines changed: 59 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -3,68 +3,76 @@ title: Enumerations
33
sidebar_position: 13
44
---
55

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

9-
## Enum Declaration
8+
## Enum declaration
109

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

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`).
1613

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

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

2318
The raw value of an enum case can be accessed through the `rawValue` field.
2419

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

3122
Enum cases can be compared using the equality operators `==` and `!=`.
3223

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
3925

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`):
4227

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
4537
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:
62+
63+
```cadence
64+
let nothing = Color(rawValue: 5) // is `nil`
65+
```
66+
67+
Enum cases can be compared:
68+
69+
```cadence
70+
Color.red == Color.red // is `true`
71+
Color(rawValue: 1) == Color.green // is `true`
72+
```
73+
74+
Different enum cases are not the same:
75+
76+
```cadence
77+
Color.red != Color.blue // is `true`
78+
```

0 commit comments

Comments
 (0)