What is an anonymous Enum in D?
Named enum
Named Enums are defined named constant values and are declared using the enum keyword.
Syntax
enum enum_name {
enumeration list
}
Example
import std.stdio;// Initializedenum Cars{ bmw , toyota, rollsroyce, mazda, lexus, subaru, mercedes };int main(string[] args) {writefln("Toyota : %d", Cars.toyota);writefln("Lexus : %d", Cars.lexus);return 0;}
Explanation
In the example above, we get some of the enum element’s index.
-
Line 6: We use an
int maininstead of the normalvoid main. This is because we are working with theenum, and thestring[] argspassed will enable theenum's access value in the main function. -
Line 7 and 8: We print the index of a named
enumusing the dot notation like theCars.enumelement. Play around with the code to understand it better.
Anonymous enum
An anonymous Enum is an unnamed enum.
Syntax
enum {
enumeration list
}
Example
import std.stdio;// Initializedenum { bmw , toyota, rollsroyce, mazda, lexus, subaru, mercedes };int main(string[] args) {writefln("Toyota : %d", toyota);writefln("Lexus : %d", lexus);return 0;}
Explanation
We get some of the enum element’s index from the example above.
-
Line 6: We use an
int maininstead of the normalvoid main. This is because we are working with theenum, and thestring[] argspassed will enable theenum's value in the main function. -
Line 7 and 8: We print the index of an anonymous
enumby just calling the name of theenumelement. Play around with the code example to understand it better.