---
title: "Enumerations"
url: https://docs.zeroc.com/ice/3.8/slice/user-defined-types/enumerations
language: matlab
docs_index: https://docs.zeroc.com/llms.txt
---

> For an index of every page, see [llms.txt](https://docs.zeroc.com/llms.txt).

# Enumerations

Programming language: MATLAB. Other languages: [C++](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/enumerations/cpp.md), [C#](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/enumerations/csharp.md), [Java](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/enumerations/java.md), [JavaScript](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/enumerations/js.md), [PHP](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/enumerations/php.md), [Python](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/enumerations/python.md), [Ruby](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/enumerations/ruby.md), [Swift](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/enumerations/swift.md).

## Enumeration Syntax and Semantics

An enumeration defines a set of named values, its enumerators:

```slice
module M
{
    enum Fruit { Apple, Pear, Orange }
}
```

This definition introduces a new type named `Fruit`. 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.

> **Note**
>
> 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<sup>31</sup> - 1.

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

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

The language mapping determines whether a program can compare two enumerators for order, and whether such a comparison follows the enumerators' values or their declaration order.

> **Warning**
>
> For an application that is still using version 1.0 of the [Ice encoding](https://docs.zeroc.com/ice/3.8/encoding/basic-data-encoding.md), changing the definition of an enumerated type **may** break backward compatibility with existing applications. For more information, please refer to the [encoding rules](https://docs.zeroc.com/ice/3.8/encoding/basic-data-encoding.md) for enumerators.

## Language Mapping

A Slice enumeration maps to the corresponding enumeration in MATLAB. For example:

```slice
enum Fruit { Apple, Pear, Orange }
```

The MATLAB mapping for `Fruit` is shown below:

```matlab
classdef Fruit < uint8
    enumeration
        Apple (0)
        Pear (1)
        Orange (2)
    end
    methods(Static)
        function r = ice_getValue(v)
            switch v
                case 0
                    r = Example.Fruit.Apple;
                case 1
                    r = Example.Fruit.Pear;
                case 2
                    r = Example.Fruit.Orange;
                otherwise
                    throw(Ice.MarshalException(...
                        sprintf('enumerator value %d is out of range', v)));
            end
        end
    end
end
```

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

```matlab
f1 = Fruit.Apple;
f2 = Fruit.Orange;

if f1 == Fruit.Apple % Compare with constant
    % ...
end

if f1 == f2          % Compare two enums
    % ...
end

switch f2            % Switch on enum
    case Fruit.Apple
        % ...
    case Fruit.Pear
        % ...
    case Fruit.Orange
        % ...
end
```

The generated enumeration derives from `uint8` when its largest enumerator value is 255 or less, and from `int32` otherwise. You obtain the value of an enumerator by converting it to this base type, here with the `uint8` function:

```matlab
val = uint8(Fruit.Pear);
assert(val == 1);
```

To convert an integer into its equivalent enumerator, call the `ice_getValue` function:

```matlab
f = Fruit.ice_getValue(2);
assert(f == Fruit.Orange);
```

This function throws an exception if the given integer does not match any of the 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:

```matlab
classdef Fruit < uint8
    enumeration
        Apple (0)
        Pear (3)
        Orange (4)
    end
    % ...
end
```

## See Also

- [Constants and Literals](https://docs.zeroc.com/ice/3.8/slice/constants-and-literals/matlab.md)
