From edf11ebf021dba897e5419cca53de3b652670799 Mon Sep 17 00:00:00 2001 From: Kevin Atkinson Date: Sun, 15 Jan 2012 17:10:59 -0700 Subject: [PATCH] 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. --- doc/tutorial/data.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/doc/tutorial/data.md b/doc/tutorial/data.md index 678714d388b..7796da21382 100644 --- a/doc/tutorial/data.md +++ b/doc/tutorial/data.md @@ -103,6 +103,24 @@ equivalent to a C enum: This will define `north`, `east`, `south`, and `west` as constants, all of which have type `direction`. +When the enum is is C like, that is none of the variants have +parameters, it is possible to explicit set the discriminator values to +an integer value: + + enum color { + red = 0xff0000; + green = 0x00ff00; + blue = 0x0000ff; + } + +If an explicit discriminator is not specified for a variant, the value +defaults to the value of the previous variant plus one. If the first +variant does not have a discriminator, it defaults to 0. For example, +the value of `north` is 0, `east` is 1, etc. + +When an enum is C-like the `as` cast operator can be used to get the +discriminator's value. + There is a special case for enums with a single variant. These are