D Programming - Enums



An enumeration is used for defining named constant values. An enumerated type is declared using the enum keyword.

The enum Syntax

The simplest form of an enum definition is the following −

enum enum_name {  
   enumeration list 
}

Where,

  • The enum_name specifies the enumeration type name.

  • The enumeration list is a comma-separated list of identifiers.

Each of the symbols in the enumeration list stands for an integer value, one greater than the symbol that precedes it. By default, the value of the first enumeration symbol is 0. For example −

enum Days { sun, mon, tue, wed, thu, fri, sat };

Example

The following example demonstrates the use of enum variable −

import std.stdio;

enum Days { sun, mon, tue, wed, thu, fri, sat };

int main(string[] args) {
   Days day;

   day = Days.mon;
   writefln("Current Day: %d", day); 
   writefln("Friday : %d", Days.fri); 
   return 0;
}

When the above code is compiled and executed, it produces the following result −

Current Day: 1 
Friday : 5

In the above program, we can see how an enumeration can be used. Initially, we create a variable named day of our user defined enumeration Days. Then we set it to mon using the dot operator. We need to use the writefln method to print the value of mon that is been stored. You also need specify the type. It is of the type integer, hence we use %d for printing.

Named Enums Properties

The above example uses a name Days for the enumeration and is called named enums. These named enums have the following properties −

  • Init − It initializes the first value in the enumeration.

  • min − It returns the smallest value of enumeration.

  • max − It returns the largest value of enumeration.

  • sizeof − It returns the size of storage for enumeration.

Let us modify the previous example to make use of the properties.

import std.stdio;

// Initialized sun with value 1 
enum Days { sun = 1, mon, tue, wed, thu, fri, sat };

int main(string[] args) { 
   writefln("Min : %d", Days.min); 
   writefln("Max : %d", Days.max);
   writefln("Size of: %d", Days.sizeof); 
   return 0; 
}

When the above code is compiled and executed, it produces the following result −

Min : 1
Max : 7
Size of: 4

Anonymous Enum

Enumeration without name is called anonymous enum. An example for anonymous enum is given below.

import std.stdio; 
 
// Initialized sun with value 1 
enum { sun , mon, tue, wed, thu, fri, sat }; 
 
int main(string[] args) { 
   writefln("Sunday : %d", sun); 
   writefln("Monday : %d", mon); 
   return 0; 
}

When the above code is compiled and executed, it produces the following result −

Sunday : 0
Monday : 1

Anonymous enums work pretty much the same way as named enums but they do not have the max, min, and sizeof properties.

Enum with Base Type Syntax

The syntax for enumeration with base type is shown below.

enum :baseType {  
   enumeration list 
}

Some of the base types includes long, int, and string. An example using long is shown below.

import std.stdio;
  
enum : string { 
   A = "hello", 
   B = "world", 
} 
  
int main(string[] args) { 
   writefln("A : %s", A); 
   writefln("B : %s", B); 
   
   return 0; 
}

When the above code is compiled and executed, it produces the following result −

A : hello
B : world

More Features

Enumeration in D provides features like initialization of multiple values in an enumeration with multiple types. An example is shown below.

import std.stdio;
  
enum { 
   A = 1.2f,  // A is 1.2f of type float 
   B,         // B is 2.2f of type float 
   int C = 3, // C is 3 of type int 
   D          // D is 4 of type int 
}
  
int main(string[] args) { 
   writefln("A : %f", A); 
   writefln("B : %f", B); 
   writefln("C : %d", C); 
   writefln("D : %d", D);  
   return 0; 
}

When the above code is compiled and executed, it produces the following result −

A : 1.200000
B : 2.200000
C : 3
D : 4
Advertisements