zscript-doc/language/Enumerations.md

59 lines
1.2 KiB
Markdown
Raw Permalink Normal View History

2019-04-02 06:36:03 -07:00
<!-- vim-markdown-toc GFM -->
2019-08-14 03:31:41 -07:00
* [Enumeration Definitions](#enumeration-definitions)
* [Example: Enumeration definitions](#example-enumeration-definitions)
2019-04-02 06:36:03 -07:00
<!-- vim-markdown-toc -->
2019-08-14 03:31:41 -07:00
# Enumeration Definitions
2019-04-02 06:36:03 -07:00
An enumeration is a list of named numbers, which by default will be incremental
from 0. By default they decay to the type `int`, but the default decay type can
be set manually.
An enumeration definition takes the form:
```
enum Identifier $[ : Integer-type]$
{
2019-08-14 03:38:33 -07:00
$[Enumerator $[ , Enumerator]$... $[ , ]$]$
2019-04-02 06:36:03 -07:00
}
```
Optionally followed by a semicolon. `Integer-type` in this context is any valid
integral type name.
Enumerators can either be incremental (from the last enumerator or 0 if there
is none) or explicitly set with the syntax `Identifier = Expression`.
Enumerators must be followed by a comma unless it is the end of the list.
Enumerator syntax is:
```
Identifier $[ = Expression]$
```
## Example: Enumeration definitions
```
2019-04-05 16:34:55 -07:00
// Basic enumeration.
2019-04-02 06:36:03 -07:00
enum MyCoolEnum
{
2019-08-14 03:38:33 -07:00
A_THING, // Has value int(0) ...
BEES, // ... 1 ...
CALCIUM, // ... 2 ...
DEXTROSE, // ... and 3.
2019-04-02 06:36:03 -07:00
}
2019-04-05 16:34:55 -07:00
// Less trivial example.
2019-04-02 06:36:03 -07:00
enum MyCoolerEnum : int16
{
2019-08-14 03:38:33 -07:00
A = 500, // Has value int16(500),
B, // 501,
C = 200,
D, // 201,
E, // and 202.
2019-04-05 16:34:55 -07:00
}
2019-04-02 06:36:03 -07:00
```
<!-- EOF -->