Question

In most popular programming languages like Java and C# there is a way to define enums, which are essentially datatypes with a fixed set of values, e.g. DayOfWeek.

The problem is, given a value, e.g. DayOfWeek.Monday, how do I get the next value of the enum, in this particular case DayOfWeek.Tuesday?

I realize that not all enums are ordered and there might be different kinds of orders for them (cyclic, partial etc.), but a simple next operation would be sufficient in most cases. In fact, since the set of values in a enum is fixed and limited, this could be done completely declaratively, assuming there is a means in the language to do so. However in most programming languages that is simply not possible, at least not as easily as it could be in theory.

So, my question is: why is that so? What are the reasons for not providing a simple syntax for declaring the next value for a enum? I suggest, in C# or Java this could even be done with a special attribute or annotation, resp. But there are none, as far as I know.

I am explicitly not asking for workarounds; I know that there are alternative solutions. I just want to know why I have to employ a workaround in the first place.

Was it helpful?

Solution

Why isn't there a next operation on enums?

It is difficult to generalize.

It is an decision made by each programming language designer / team. But note that some programming languages do provide a "next" operation for enumerated types.

In Pascal, the succ function returns the next value of an enumerated type, and the pred function returns the previous value. But this only works for classic enumerated types. C-style enumerated types have "holes" in the type domain, and the succ and pred functions are not allowed.

Reference: http://www.freepascal.org/docs-html/ref/refse12.html#QQ2-27-32

And, in fact, that gives a clue as to why many languages with enumeration / enumerated types don't have a "next" operation. It is prohibitively expensive when you can't implement "next" by using integer addition under the hood.


Of course, in the languages that don't support next directly, it is always possible to implement it for yourself ... if you need it ... as some kind of helper method / function.

OTHER TIPS

In Java, the enum is a reference type (don't let that little 'e' fool you). You can do neat things with it like define methods. A classic example of that is the Operation enum being defined as:

public enum Operation {
  PLUS   { double eval(double x, double y) { return x + y; } },
  MINUS  { double eval(double x, double y) { return x - y; } },
  TIMES  { double eval(double x, double y) { return x * y; } },
  DIVIDE { double eval(double x, double y) { return x / y; } };

  // Do arithmetic op represented by this constant
  abstract double eval(double x, double y);
}
//Elsewhere:
Operation op = Operation.PLUS;
double two = op.eval(1, 1);

And you can see... yep, it has methods and so its not a primitive. They are Objects.

There is also some magic that happens in the compiler as described in the Java Enum Tutorial:

The compiler automatically adds some special methods when it creates an enum. For example, they have a static values method that returns an array containing all of the values of the enum in the order they are declared. This method is commonly used in combination with the for-each construct to iterate over the values of an enum type. For example, this code from the Planet class example below iterates over all the planets in the solar system.

And I'll certainly agree that its annoying that this isn't mentioned in the javadocs.

The values that are returned from the values() call is an array that one can then iterate over.

class Demo {
    enum DayOfWeek {
        U, M, T, W, R, F, S
    }

    public static void main (String[] args) {
        for (DayOfWeek day : DayOfWeek.values()) {
            System.out.println(day.toString());
        }
    }
}

This prints out:

U
M
T
W
R
F
S

Run this code at ideone

Why no next? Well, given the ability to get the values() array it fills all the use cases for the next() method and is a bit more powerful.

If you really do want the next() method it takes takes a little bit of a hoop to get the array into an Iterator object (which is itself a replacement for the Enumeration type - see the javadoc):

import java.util.*;

class Demo {
    enum DayOfWeek {
        U, M, T, W, R, F, S
    }

    public static void main (String[] args) {
        Iterator days = Arrays.asList(DayOfWeek.values()).iterator();
        while(days.hasNext()) {
            System.out.println(days.next());
        }
    }
}

Run this code at ideone

So again, the reason that its not there is that its not needed. You can get an array back from the enum quite easily and iterate over that, or convert that array into an Iterator allowing you to call next().

There is also possibly the confusion that would be had trying to make the enum and Enumeration look too similar (they already look too similar) and trying to make the enum look more like the Enumeration with the same method calls would further confuse things. That said, this is just speculation.

Fundamentally, the Java enum is much like the C enum with the ability to have some methods on the side. Its a bit more constrained you can't have them be specific numbers behind the scenes as you can in C:

/* This is C */
enum cardsuit {
    CLUBS    = 1,
    DIAMONDS = 2,
    HEARTS   = 4,
    SPADES   = 8
};

People sometimes point to Pascal and say succ can be called on an enum there, why not in Java? Pascal enums are ordinal types while C enums are not. Java borrows from C rather than Pascal.

There's nothing saying that you can't define your own next() or prev() function. Its rather easy to do after getting around an annoyance of forward references.

class Demo {
    enum DayOfWeek {
        U, M, T, W, R, F, S;

        private DayOfWeek n;
        private DayOfWeek p;

        static {
            U.n = M; U.p = S;
            M.n = T; M.p = U;
            T.n = W; T.p = M;
            W.n = R; W.p = T;
            R.n = F; R.p = W;
            F.n = S; F.p = R;
            S.n = U; S.p = F;
        }

        public DayOfWeek next() { return this.n; }
        public DayOfWeek prev() { return this.p; }

    }

    public static void main (String[] args) {
        System.out.println(DayOfWeek.M.next());
        System.out.println(DayOfWeek.U.prev());
    }
}
T
S

So...

  1. The enum was borrowed from the C tradition of an enum, not the Pascal tradition of ordinal types
  2. They didn't want to confuse enum with Enumeration
  3. You have the ability to get all the values back easily with an iterable type
  4. You can define a next() method that works any way you want it to.

It is because of the nature of enums (enum in Java), (enum in C#). They are just fixed constants in the language, holding essential data (sometimes just their own names or just identity), not a kind of list. Therefore if you need this kind of logic (which might be applicable to many cases), then you actually have to create the logic.

Apart of being some sort of linked list, it can be singleton too (see: Implementation Example: Using enum). It is more matter of use. Sadly enums (at least in Java) in this way tricker since you can't extend them or define an interface directly for them.

It is an open debate if it is good practice or not to use enums for some sort of constant list or singleton, the fact is, you can. I would vote against using it unless it is widely used pattern in the project. Smells like hacking for me if it is used locally.

enums are replaced with their constant values in the executable. they can not be traversed at runtime.

in C enum are part of the source code and the compiler replaces the enum with the constant int.

     // C source - enum gets replaced with constant
enum _tenum { A, B, C};
_tenum ft;
ft = B;
     // assembler enum is gone. only constant value 1 remains
mov DWORD PTR _ft$[ebp], 1

if (ft == B) {

cmp DWORD PTR _ft$[ebp], 1
jne SHORT $LN1@main

in Java enum are also part of the source file and the byte code generator replaces the enum with the public final static value.

Here is an explaination http://boyns.blogspot.com/2008/03/java-15-explained-enum.html

Technically speaking, there is a way to perform a next operation in both languages, though it varies between Java and C#.

C#

In C#, enumerations are number types at their core. You can simply perform addition with integers:

var day = DayOfWeek.Sunday;
foreach(var n in Enumerable.Range(0, 7))
    Console.WriteLine("Day of week: {0}", day + n);

note: the above works the same way with day++

Output:

Day of week: Sunday
Day of week: Monday
Day of week: Tuesday
Day of week: Wednesday
Day of week: Thursday
Day of week: Friday
Day of week: Saturday

Java

Java allows you to define enums as full-blown object types. As a result, you cannot simply increment it. However, StackOverflow shows several alternatives.

TL;DR: there's a somewhat hackish way of incrementing an enum, but the suggested way is to implement a next() function in the enum.

@dan-lyons answer for C# is okay, but it only works when:

  • the enum is defined using consecutive integer values
  • you know how many values there are
  • you know what their integer equivalents are.

Instead, I would suggest using Enum.GetValues() and iterating the collection it returns.

enum MyEnum
{
    Value1 = -4,
    Value2 = 0,
    Value3,       // = 1
    Value4,       // = 2
    Value5 = 100,
}

...

// We need to use Cast<>() because GetValues() only returns
// a non-generic Array, which can hold any type of object.
var values = Enum.GetValues(typeof(MyEnum)).Cast<MyEnum>();
foreach(var value in values)
{
    Console.WriteLine($"{value} = {(int)value}");
}

This will print out all explicitly defined values by their coded name and their integer equivalents.

Value1 = -4
Value2 = 0
Value3 = 1
Value4 = 2
Value5 = 100

You don't need to know the values or how many there are.

Providing this kind of behavior on enums would only facilitate a hack.

Enums are not arrays. They may technically be implemented as arrays but their logical meaning is a limited number of grouped constants that may be used in a type-safe manner. There is no logical sequental relation between group members.

An enum type definition is not a list, it is not even a collection, so individual members do not have an index, the order they appear in your source code is logically meaningless.

With their DayOfWeek implementation, Microsoft is being pragmatic and causing confusion as to what an enum is and should be used for:

If cast to an integer, its value ranges from zero (which indicates DayOfWeek.Sunday) to six (which indicates DayOfWeek.Saturday)

How convenient... and wrong. The proper way would have been to offer a Week class with a static method:

static DayOfWeek Day(DayOfWeek pivot, int offset)

Then there would not be a roll-over issue and one would not have to abuse the enum.

Before Java there was C, C++ and Pascal. Enumerations in C and C++ could have wildly different values - they did not need to be contiguous integers. This had the advantage that you could define an enum for things like Windows message codes, which were a sparse set of non-contiguous integers.

(Borland) Pascal took the other approach, where all enumerations had contiguous ordinal values starting from zero. This meant set arithmetic was easy (e.g. Define Jan..Dec as an enum, then SummerMonths as a set of months (July, Aug, September). It was then legal to write if (month in SummerMonths)... It was also memory efficient, since enums could be bit-mapped.

For better or worse, Java went down the C/C++ route. For that reason, a next() operation does not make good sense.

Speaking for Java, what you are actually looking for is Enumeration (which is more like iterator) which has the nextElement support.

enum is a data type which allows variable to have a set of predefined values.

Licensed under: CC-BY-SA with attribution
scroll top