Skip to main content
Skip table of contents

Enumerations

Enumeration Syntax and Semantics

A Slice enumerated type definition looks identical to C++:

SLICE
module M
{
    enum Fruit { Apple, Pear, Orange }
}

This definition introduces a type named Fruit that becomes a new type in its own right. Slice guarantees that the values of enumerators increase from left to right, so Apple compares less than Pear in every language mapping. By default, the first enumerator has a value of zero, with sequentially increasing values for subsequent enumerators.

A Slice enum type introduces a new namespace scope, so the following is legal:

SLICE
module M
{
    enum Fruit { Apple, Pear, Orange }
    enum ComputerBrands { Apple, Dell, HP, Lenovo }
}

The example below shows how to refer to an enumerator from a different scope:

SLICE
module M
{
    enum Color { Red, Green, Blue }
}

module N
{
    struct Pixel
    {
        M::Color c = Blue;
    }
}

Slice does not permit empty enumerations.

In Ice releases prior to Ice 3.7, an enum type did not create a new namespace and its enumerators were in the same namespace as the enum type itself. With these releases, you had to select longer enumerator names to avoid a naming clash.

Custom Enumerator Values

Slice also permits you to assign custom values to enumerators:

SLICE
const int PearValue = 7;
enum Fruit { Apple = 0, Pear = PearValue, Orange }

Custom values must be unique and non-negative, and may refer to Slice constants of integer types. If no custom value is specified for an enumerator, its value is one greater than the enumerator that immediately precedes it. In the example above, Orange has the value 8.

The maximum value for an enumerator value is the same as the maximum value for int, 2 31 - 1.

Slice does not require custom enumerator values to be declared in increasing order:

SLICE
enum Fruit { Apple = 5, Pear = 3, Orange = 1 }   // Legal

Note however that when there is an inconsistency between the declaration order and the numerical order of the enumerators, the behavior of comparison operations may vary between language mappings.

For an application that is still using version 1.0 of the Ice encoding, changing the definition of an enumerated type may break backward compatibility with existing applications. For more information, please refer to the encoding rules for enumerators.

Language Mapping

JavaScript does not have an enumerated type, so a Slice enumeration is emulated using JavaScript objects where each enumerator is an instance of the same type. For example:

SLICE
enum Fruit { Apple, Pear, Orange }

The generated code is equivalent to the following JavaScript code:

JS
class Fruit { ... }
Fruit.Apple = new Fruit("Apple", 0);
Fruit.Pear = new Fruit("Pear", 1);
Fruit.Orange = new Fruit("Orange", 2);

And the generated TypeScript definition for the generated code looks like:

TYPESCRIPT
class Fruit
{
    static readonly Apple:Fruit;
    static readonly Pear:Fruit;
    static readonly Orange:Fruit;
        
    static valueOf(value:number):Fruit;
  
    equals(other:any):boolean;
    hashCode():number;
    toString():string;
        
    readonly name:string;
    readonly value:number;
}

Each enumerator defines name and value properties that supply the enumerator's name and ordinal value, respectively. Enumerators also define hashCodeequals and toString methods, and the enumerated type itself defines a valueOf method that converts ordinal values into their corresponding enumerators.

Suppose we modify the Slice definition to include a custom enumerator value:

SLICE
enum Fruit { Apple, Pear = 3, Orange }

The generated code changes accordingly:

JS
class Fruit = { ... };
Fruit.Apple = new Fruit("Apple", 0);
Fruit.Pear = new Fruit("Pear", 3);
Fruit.Orange = new Fruit("Orange", 4);

Given the above definitions, we can use enumerated values as follows:

JS
const f1 = Fruit.Apple;
const f2 = Fruit.Orange;

if (f1 === Fruit.Apple) { // Compare with constant
    // ...
}

if (f1 === f2) {         // Compare two enums
    // ...
}

switch(f2) {             // Switch on enum
    case Fruit.Apple:
        // ...
        break;
    case Fruit.Pear:
        // ...
        break;
    case Fruit.Orange:
        // ...
        break;
}

// Convert an ordinal value to its enumerator, or undefined if no match
const f = Fruit.valueOf(3);
console.log(f.name + " = " + f.value); // Outputs "Pear = 3"

TypeScript has an enumerated type, but the TypeScript definitions must match the JavaScript generated code, using enumerated types in TypeScript is identical to use them in JavaScript.

See Also
JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.