Learning Center

Learning Center

Learn Menu

Enum

Enum is another type of entity. It's expressed as a block statement starting with the enum keyword, followed by an id, data type and curly brackets to hold the body. Check out an empty Color enum example below.

  enum Color: string {

}

Cases

An enum body consists of zero or more cases. Each case represents a value of the enum data type. The syntax is the following: case $id = $value. Case id is required, while value is optional. Hence this syntax would be valid: case $id.

Supported data types

OneGen currently supports enums of the following data types.

int

This is the most common and well-known enum type. As customary, if case value is missing, a number starting from zero will be used. Let's portray this behavior in the two examples below. The generated code would be identical.

with automatic values

  enum Permission: int {
    case Read
    case Write
    case Admin
}

with custom values

  enum Permission: int {
    case Read = 0
    case Write = 1
    case Admin = 2
}

string

Each enum case may store an arbitrary string value. If there's no value, the id will be used as a value, see the examples below.

with automatic values

  enum Color: string {
    case GREEN
    case RED
    case BLUE
}

with custom values

  enum Color: string {
    case GREEN = "GREEN"
    case RED = "RED"
    case BLUE = "BLUE"
}

We use cookies to track activity using Google Analytics & reCAPTCHA. It helps us understand what our visitors like about our product and how they interact with our website. For more information, check out our Privacy Policy.