Skip to content

Commit edf11eb

Browse files
kevinamarijnh
authored andcommitted
In the tutorial, document that C-like enums can have the discriminator
values set and that it is possible to cast them to scalar values.
1 parent 96f1eda commit edf11eb

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

doc/tutorial/data.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,24 @@ equivalent to a C enum:
103103
This will define `north`, `east`, `south`, and `west` as constants,
104104
all of which have type `direction`.
105105

106+
When the enum is is C like, that is none of the variants have
107+
parameters, it is possible to explicit set the discriminator values to
108+
an integer value:
109+
110+
enum color {
111+
red = 0xff0000;
112+
green = 0x00ff00;
113+
blue = 0x0000ff;
114+
}
115+
116+
If an explicit discriminator is not specified for a variant, the value
117+
defaults to the value of the previous variant plus one. If the first
118+
variant does not have a discriminator, it defaults to 0. For example,
119+
the value of `north` is 0, `east` is 1, etc.
120+
121+
When an enum is C-like the `as` cast operator can be used to get the
122+
discriminator's value.
123+
106124
<a name="single_variant_enum"></a>
107125

108126
There is a special case for enums with a single variant. These are

0 commit comments

Comments
 (0)