\chapter{Related Work}
\label{s:RelatedWork}

\begin{comment}
An algebraic data type (ADT) can be viewed as a recursive sum of product types.
A sum type lists values as members.
A member in a sum type definition is known as a data constructor.
For example, C supports sum types union and enumeration (enum).
An enumeration in C can be viewed as the creation of a list of zero-arity data constructors.
A union instance holds a value of one of its member types.
Defining a union does not generate new constructors.
The definition of member types and their constructors are from the outer lexical scope.

In general, an \newterm{algebraic data type} (ADT) is a composite type, \ie, a type formed by combining other types.
Three common classes of algebraic types are \newterm{array type}, \ie homogeneous types, \newterm{product type}, \ie heterogeneous tuples and records (structures), and \newterm{sum type}, \ie tagged product-types (unions).
Enumerated types are a special case of product/sum types with non-mutable fields, \ie initialized (constructed) once at the type's declaration, possible restricted to compile-time initialization.
Values of algebraic types are access by subscripting, field qualification, or type (pattern) matching.
\end{comment}

Enumeration-like features exist in many popular programming languages, both past and present, \eg Pascal~\cite{Pascal}, Ada~\cite{Ada}, \Csharp~\cite{Csharp}, OCaml~\cite{OCaml} \CC, Go~\cite{Go}, Haskell~\cite{Haskell}, Java~\cite{Java}, Modula-3~\cite{Modula-3}, Rust~\cite{Rust}, Swift~\cite{Swift}, Python~\cite{Python}.
Among theses languages, there are a large set of overlapping features, but each language has its own unique extensions and restrictions.

\section{Pascal}
\label{s:Pascal}

Classic Pascal has the \lstinline[language=Pascal]{const} aliasing declaration binding a name to a constant literal/expression.
\begin{pascal}
const one = 0 + 1;   Vowels = set of (A,E,I,O,U);   NULL = NIL;
		 PI = 3.14159;   Plus = '+';   Fred = 'Fred';
\end{pascal}
As stated, this mechanism is not an enumeration because there is no specific type (pseudo enumeration).
Hence, there is no notion of a (possibly ordered) set, modulo the \lstinline[language=pascal]{set of} type.
The type of each constant name (enumerator) is inferred from the constant-expression type.

Free Pascal~\cite[\S~3.1.1]{FreePascal} is a modern, object-oriented version of classic Pascal, with a C-style enumeration type.
Enumerators must be assigned in ascending numerical order with a constant expression and the range can be non-consecutive.
\begin{pascal}
Type EnumType = ( one, two, three, forty @= 40@, fortyone );
\end{pascal}
Pseudo-functions @Pred@ and @Succ@ can only be used if the range is consecutive.
The underlying type is an implementation-defined integral-type large enough to hold all enumerated values; it does not have to be the smallest possible type.
The integral size can be explicitly specified using compiler directive @$PACKENUM@~$N$, where $N$ is the number of bytes, \eg:
\begin{pascal}
Type @{$\color{red}\$$PACKENUM 1}@ SmallEnum = ( one, two, three );
	    @{$\color{red}\$$PACKENUM 4}@ LargeEnum = ( BigOne, BigTwo, BigThree );
Var S : SmallEnum; { 1 byte }
	  L : LargeEnum; { 4 bytes}
\end{pascal}


\section{Ada}

An Ada enumeration type is a set of ordered unscoped identifiers (enumerators) bound to \emph{unique} \newterm{literals}.\footnote{%
Ada is \emph{case-insensitive} so identifiers may appear in multiple forms and still be the same, \eg \lstinline{Mon}, \lstinline{moN}, and \lstinline{MON} (a questionable design decision).}
\begin{ada}
type Week is ( Mon, Tue, Wed, Thu, Fri, Sat, Sun ); -- literals (enumerators)
\end{ada}
Object initialization and assignment are restricted to the enumerators of this type.
While Ada enumerators are unscoped, like C, Ada enumerators are overloadable.
\begin{ada}
type RGB is ( @Red@, @Green@, Blue );
type Traffic_Light is ( @Red@, Yellow, @Green@ );
\end{ada}
Like \CFA, Ada uses an advanced type-resolution algorithm, including the left-hand side of assignment, to disambiguate among overloaded identifiers.
\VRef[Figure]{f:AdaEnumeration} shows how ambiguity is handled using a cast, \ie \lstinline[language=ada]{RGB'(Red)}.

\begin{figure}
\begin{ada}
with Ada.Text_IO; use Ada.Text_IO;
procedure test is
	type RGB is ( @Red@, Green, Blue );
	type Traffic_Light is ( @Red@, Yellow, Green );         -- overload
	procedure @Red@( Colour : RGB ) is begin            -- overload
		Put_Line( "Colour is " & RGB'Image( Colour ) );
	end Red;
	procedure @Red@( TL : Traffic_Light ) is begin       -- overload
		Put_Line( "Light is " & Traffic_Light'Image( TL ) );
	end Red;
begin
	@Red@( Blue );				 -- RGB
	@Red@( Yellow );				-- Traffic_Light
	@Red@( @RGB'(Red)@ );		-- ambiguous without cast
end test;
\end{ada}
\caption{Ada Enumeration Overload Resolution} 
\label{f:AdaEnumeration} 
\end{figure}

Enumerators without initialization are auto-initialized from left to right, starting at zero, incrementing by 1.
Enumerators with initialization must set \emph{all} enumerators in \emph{ascending} order, \ie there is no auto-initialization.
\begin{ada}
type Week is ( Mon, Tue, Wed, Thu, Fri, Sat, Sun );
for Week use ( Mon => 0, Tue => 1, Wed => 2, Thu => @10@, Fri => 11, Sat => 14, Sun => 15 );
\end{ada}
The enumeration operators are the equality and relational operators, @=@, @/=@, @<@, @<=@, @=@, @/=@, @>=@, @>@, where the ordering relationship is given implicitly by the sequence of acsending enumerators.

Ada provides an alias mechanism, \lstinline[language=ada]{renames}, for aliasing types, which is useful to shorten package identifiers.
\begin{ada}
OtherRed : RGB renames Red;
\end{ada}
which suggests a possible \CFA extension to @typedef@.
\begin{cfa}
typedef RGB.Red OtherRed;
\end{cfa}

There are three pairs of inverse enumeration pseudo-functions (attributes): @'Pos@ and @'Val@, @'Enum_Rep@ and @'Enum_Val@, and @'Image@ and @'Value@,
\begin{cquote}
\setlength{\tabcolsep}{15pt}
\begin{tabular}{@{}ll@{}}
\begin{ada}
RGB'Pos( Red ) = 0;
RGB'Enum_Rep( Red ) = 10;
RGB'Image( Red ) = "RED";
\end{ada}
&
\begin{ada}
RGB'Val( 0 ) = Red
RGB'Enum_Val( 10 ) =  Red
RGB'Value( "Red" ) =  Red
\end{ada}
\end{tabular}
\end{cquote}
These attributes are important for IO.
An enumeration type @T@ also has the following attributes: @T'First@, @T'Last@, @T'Range@, @T'Pred@, @T'Succ@, @T'Min@, and @T'Max@, producing an intuitive result based on the attribute name.

Ada allows the enumerator label to be a character constant.
\begin{ada}
type Operator is ( '+', '-', '*', '/' );
\end{ada}
which is syntactic sugar for the label and not character literals from the predefined type @Character@.
The purpose is strictly readability using character literals rather than identifiers.
\begin{ada}
Op : Operator := '+';
if Op = '+' or else Op = '-' then ... ;
elsif Op = '*' or else Op = '/' then ... ; end if;
\end{ada}
Interestingly, arrays of character enumerators can be treated as strings.
\begin{ada}
Ops : array( 0..3 ) of Operator;
Ops := @"+-*/"@;            -- string assignment to array elements
Ops := "+-" @&@ "*/";   -- string concatenation and assignment
\end{ada}
Ada's @Character@ type is defined as a character enumeration across all Latin-1 characters.

Ada's boolean type is also a special enumeration, which can be used in conditions.
\begin{ada}
type Boolean is (False, True); -- False / True not keywords
@Flag@ : Boolean;
if @Flag@ then ...    -- conditional
\end{ada}
Since only types derived from @Boolean@ can be a conditional, @Boolean@ is essentially  a builtin type.

Ada provides \emph{consecutive} subtyping of an enumeration using \lstinline[language=ada]{range}.
\begin{ada}
type Week is ( Mon, Tue, Wed, Thu, Fri, Sat, Sun );
subtype Weekday is Week @range Mon .. Fri@;
subtype Weekend is Week @range Sat .. Sun@;
Day : Week;
\end{ada}
Hence, the ordering of the enumerators is crucial to provide the necessary ranges.

An enumeration type can be used in the Ada \lstinline[language=ada]{case} (all enumerators must appear or a default) or iterating constructs.
\begin{cquote}
\setlength{\tabcolsep}{15pt}
\begin{tabular}{@{}ll@{}}
\begin{ada}
case Day is
	when @Mon .. Fri@ => ... ;
	when @Sat .. Sun@ => ... ;
end case;
\end{ada}
&
\begin{ada}
case Day is
	when @Weekday@ => ... ;  -- subtype ranges
	when @Weekend@ => ... ;
end case;
\end{ada}
\end{tabular}
\end{cquote}

\begin{cquote}
\setlength{\tabcolsep}{12pt}
\begin{tabular}{@{}lll@{}}
\begin{ada}
for Day in @Mon .. Sun@ loop
	...
end loop;
\end{ada}
&
\begin{ada}
for Day in @Weekday@ loop
	...
end loop;
\end{ada}
&
\begin{ada}
for Day in @Weekend@ loop
	...
end loop;
\end{ada}
\end{tabular}
\end{cquote}

An enumeration type can be used as an array dimension and subscript.
\begin{ada}
Lunch : array( @Week@ ) of Time;
for Day in Week loop
	Lunch( @Day@ ) := ... ;       -- set lunch time
end loop;
\end{ada}


\section{\CC}
\label{s:C++RelatedWork}

\CC enumeration is largely backwards compatible with C, so it inherited C's enumerations with some modifications and additions.

\CC has aliasing using @const@ declarations, like C \see{\VRef{s:Cconst}}, with type inferencing, plus static/dynamic initialization.
(Note, a \CC @constexpr@ declaration is the same @const@ with the restriction that the initialization is a compile-time expression.)
\begin{c++}
const @auto@ one = 0 + 1;				$\C{// static initialization}$
const @auto@ NIL = nullptr;
const @auto@ PI = 3.14159;
const @auto@ Plus = '+';
const @auto@ Fred = "Fred";
const @auto@ Mon = 0, Tue = Mon + 1, Wed = Tue + 1, Thu = Wed + 1, Fri = Thu + 1,
				Sat = Fri + 1, Sun = Sat + 1;
void foo() {
	const @auto@ r = random();			$\C{// dynamic initialization}$
	int va[r];							$\C{// VLA, auto scope only}$
}
\end{c++}
Statically initialized identifiers may appear in any constant-expression context, \eg @case@.
Dynamically initialized identifiers may appear as array dimensions in @g++@, which allows variable-sized arrays.
Interestingly, global \CC @const@ declarations are implicitly marked @static@ (@r@, read-only local, rather than @R@, read-only external)
\begin{c++}
$\$$ nm test.o
0000000000000018 @r@ Mon
\end{c++}
whereas C @const@ declarations without @static@ are marked @R@.

The following non-backwards compatible changes are made \see{\cite[\S~7.2]{ANSI98:C++}}.
\begin{cquote}
Change: \CC objects of enumeration type can only be assigned values of the same enumeration type.
In C, objects of enumeration type can be assigned values of any integral type. \\
Example:
\begin{c++}
enum color { red, blue, green };
color c = 1;			 				$\C{// valid C, invalid C++}$
\end{c++}
\textbf{Rationale}: The type-safe nature of \CC. \\
\textbf{Effect on original feature}: Deletion of semantically well-defined feature. \\
\textbf{Difficulty of converting}: Syntactic transformation. (The type error produced by the assignment can be automatically corrected by applying an explicit cast.) \\
\textbf{How widely used}: Common.
\end{cquote}

\begin{cquote}
Change: In \CC, the type of an enumerator is its enumeration.
In C, the type of an enumerator is @int@. \\
Example:
\begin{c++}
enum e { A };
sizeof(A) == sizeof(int)		 		$\C{// in C}$
sizeof(A) == sizeof(e)		 			$\C{// in C++}$
/* and sizeof(int) is not necessary equal to sizeof(e) */
\end{c++}
\textbf{Rationale}: In \CC, an enumeration is a distinct type. \\
\textbf{Effect on original feature}: Change to semantics of well-defined feature. \\
\textbf{Difficulty of converting}: Semantic transformation. \\
\textbf{How widely used}: Seldom. The only time this affects existing C code is when the size of an enumerator is taken.
Taking the size of an enumerator is not a common C coding practice.
\end{cquote}
Hence, the values in a \CC enumeration can only be its enumerators (without a cast).
While the storage size of an enumerator is up to the compiler, there is still an implicit cast to @int@.
\begin{c++}
enum E { A, B, C };
E e = A;
int i = A;    i = e; 					$\C{// implicit casts to int}$
\end{c++}
\CC{11} added a scoped enumeration, \lstinline[language=c++]{enum class} (or \lstinline[language=c++]{enum struct}), where the enumerators are accessed using type qualification.
\begin{c++}
enum class E { A, B, C };
E e = @E::@A;	 						$\C{// qualified enumerator}$
e = B;	 								$\C{// error: B not in scope}$
\end{c++}
\CC{20} supports explicit unscoping with a \lstinline[language=c++]{using enum} declaration.
\begin{c++}
enum class E { A, B, C };
@using enum E;@
E e = A;    e = B;						$\C{// direct access}$
\end{c++}
\CC{11} added the ability to explicitly declare the underlying \emph{integral} type for \lstinline[language=c++]{enum class}.
\begin{c++}
enum class RGB @: long@ { Red, Green, Blue };
enum class rgb @: char@ { Red = 'r', Green = 'g', Blue = 'b' };
enum class srgb @: signed char@ { Red = -1, Green = 0, Blue = 1 };
\end{c++}
There is no implicit conversion from the \lstinline[language=c++]{enum class} type to its declared type.
\begin{c++}
rgb crgb = rgb::Red;
char ch = rgb::Red;   ch = crgb;		$\C{// error}$
\end{c++}
Finally, enumerations can be used in the @switch@ statement but there is no mechanism to iterate through an enumeration.
An enumeration type cannot declare an array dimension but can be used as a subscript.
There is no mechanism to subtype or inherit from enumerations.


\section{C\raisebox{-0.7ex}{\LARGE$^\sharp$}\xspace} % latex bug: cannot use \relsize{2} so use \LARGE
\label{s:Csharp}

% https://www.tutorialsteacher.com/codeeditor?cid=cs-mk8Ojx

\Csharp is a dynamically-typed programming-language with a scoped, integral enumeration-type similar to the C/\CC enumeration.
\begin{csharp}
enum Weekday : byte { Mon, Tue, Wed, Thu@ = 10@, Fri, Sat, Sun@,@ };
\end{csharp}
The default underlying type is @int@, with auto-incrementing, implicit/explicit initialization, terminator comma, and optional integral typing (default @int@).
A method cannot be defined in an enumeration type.
As well, there is an explicit bidirectional conversion between an enumeration and its integral type, and an implicit conversion to the enumerator label in display contexts.
\begin{csharp}
int day = (int)Weekday.Fri;		$\C{// day == 10}$
Weekday weekday = (Weekdays)42;		$\C{// weekday == 42, logically invalid}$
Console.WriteLine( Weekday.Fri ); $\C{// print Fri}$
string mon = Weekday.Mon.ToString(); $\C{// mon == "Mon"}$
\end{csharp}

The @Enum.GetValues@ pseudo-method retrieves an array of the enumeration constants for looping over an enumeration type or variable (expensive operation).
\begin{csharp}
foreach ( Weekday constant in @Enum.GetValues@( typeof(Weekday) ) ) {
	Console.WriteLine( constant + " " + (int)constant ); // label, position
}
\end{csharp}

The @Flags@ attribute creates a bit-flags enumeration, allowing bitwise operators @&@, @|@, @~@ (complement), @^@ (xor).
\begin{csharp}
@[Flags]@ public enum Weekday {
	None = 0x0, Mon = 0x1, Tue = 0x2, Wed = 0x4,
	Thu = 0x8, Fri = 0x10, Sat = 0x20, Sun = 0x40,
	Weekend = @Sat | Sun@,
	Weekdays = @Mon | Tue | Wed | Thu | Fri@
}
Weekday meetings = @Weekday.Mon | Weekday.Wed@; // 0x5
\end{csharp}

\VRef[Figure]{CsharpFreeVersusClass} shows an enumeration with free routines for manipulation, and embedding the enumeration and operations into an enumeration class.
The key observation is that an enumeration class is just a structuring mechanism without any additional semantics.

% https://learn.microsoft.com/en-us/dotnet/api/system.enum?view=net-8.0

\begin{figure}
\centering
\begin{tabular}{@{}l|l@{}}
\multicolumn{1}{@{}c|}{non-object oriented} & \multicolumn{1}{c@{}}{object oriented} \\
\hline
\begin{csharp}
public class Program {

	enum Weekday {
		Mon, Tue, Wed, Thu, Fri, Sat, Sun };

	static bool isWeekday( Weekday wd ) {
		return wd <= Weekday.Fri;
	}
	static bool isWeekend( Weekday wd ) {
		return Weekday.Sat <= wd;
	}


	public static void Main() {
		Weekday day = Weekday.Sat;

		Console.WriteLine( isWeekday( day ) );
		Console.WriteLine( isWeekend( day ) );
	}
}
\end{csharp}
&
\begin{csharp}
public class Program {
	public @class@ WeekDay : Enumeration {
		public enum Day {
				Mon, Tue, Wed, Thu, Fri, Sat, Sun };
		public enum Day2 : Day {
				XXX, YYY };
		Day day;
		public bool isWeekday() {
			return day <= Day.Fri;
		}
		public bool isWeekend() {
			return day > Day.Fri;
		}
		public WeekDay( Day d ) { day = d; }
	}
	public static void Main() {
		WeekDay cday = new
				WeekDay( WeekDay.Day.Sat );
		Console.WriteLine( cday.isWeekday() );
		Console.WriteLine( cday.isWeekend() );
	}
}
\end{csharp}
\end{tabular}
\caption{\Csharp: Free Routine Versus Class Enumeration}
\label{CsharpFreeVersusClass}
\end{figure}


\section{Golang}

Golang provides pseudo-enumeration similar to classic Pascal \lstinline[language=pascal]{const}, binding a name to a constant literal/expression.
\begin{Go}
const ( R = 0; G; B )					$\C{// implicit: 0 0 0}$
const ( Fred = "Fred"; Mary = "Mary"; Jane = "Jane" ) $\C{// explicit: Fred Mary Jane}$
const ( S = 0; T; USA = "USA"; U; V = 3.1; W ) $\C{// type change, implicit/explicit: 0 0 USA USA 3.1 3.1}$
\end{Go}
Constant identifiers are unscoped and must be unique (no overloading).
The first enumerator \emph{must} be explicitly initialized;
subsequent enumerators can be implicitly or explicitly initialized.
Implicit initialization is the previous (predecessor) enumerator value.

Auto-incrementing is supported by the keyword \lstinline[language=Go]{iota}, available only in the \lstinline[language=Go]{const} declaration.
The \lstinline[language=Go]{iota} is a \emph{per \lstinline[language=golang]{const} declaration} integer counter, starting at zero and implicitly incremented by one for each \lstinline[language=golang]{const} identifier (enumerator).
\begin{Go}
const ( R = @iota@; G; B )				$\C{// implicit: 0 1 2}$
const ( C = @iota + B + 1@; G; Y )		$\C{// implicit: 3 4 5}$
\end{Go}
An underscore \lstinline[language=golang]{const} identifier advances \lstinline[language=Go]{iota}.
\begin{Go}
const ( O1 = iota + 1; @_@; O3; @_@; O5 ) // 1, 3, 5 
\end{Go}
Auto-incrementing stops after an explicit initialization.
\begin{Go}
const ( Mon = iota; Tue; Wed; // 0, 1, 2
	@Thu = 10@; Fri; Sat; Sun ) // 10, 10, 10, 10
\end{Go}
Auto-incrementing can be restarted with an expression containing \emph{one} \lstinline[language=Go]{iota}.
\begin{Go}
const ( V1 = iota; V2; @V3 = 7;@ V4 = @iota@; V5 ) // 0 1 7 3 4
const ( Mon = iota; Tue; Wed; // 0, 1, 2
	@Thu = 10;@ Fri = @iota - Wed + Thu - 1@; Sat; Sun ) // 10, 11, 12, 13
\end{Go}
Note, \lstinline[language=Go]{iota} is advanced for an explicitly initialized enumerator, like the underscore @_@ identifier.

Basic switch and looping are possible.
\begin{cquote}
\setlength{\tabcolsep}{15pt}
\begin{tabular}{@{}ll@{}}
\begin{Go}
day := Mon;
switch day {
  case Mon, Tue, Wed, Thu, Fri:
	fmt.Println( "weekday" );
  case Sat, Sun:
	fmt.Println( "weekend" );
}
\end{Go}
&
\begin{Go}

for i := Mon; i <= Sun; i += 1 {
	fmt.Println( i )
}



\end{Go}
\end{tabular}
\end{cquote}
However, the loop prints the values from 0 to 13 because there is no actual enumeration.


\section{Java}

Every enumeration in Java is an enumeration class.
For a basic enumeration
\begin{Java}
enum Weekday { Mon, Tue, Wed, Thu, Fri, Sat, Sun };
Weekday day = Weekday.Sat;
\end{Java}
the scoped enumerators are an ordered list of @final@ methods of type integer, ordered left to right starting at 0, increasing by 1.
The value of an enumeration instance is restricted to the enumeration's enumerators.
There is an implicit @int@ variable in the enumeration used to store the value of an enumeration instance.
The position (ordinal) and label are accessible, where the value is the same as the position.
\begin{Java}
System.out.println( day.!ordinal()! + " " + day.!name()! ); // 5 Sat
\end{Java}
There is an inverse function @valueOf@ from string to enumerator.
\begin{Java}
day = Weekday.valueOf( "Wed" );
\end{Java}
There are no implicit conversions to/from an enumerator and its underlying type.
Like \Csharp, \VRef[Figure]{f:JavaFreeVersusClass} shows the same example for an enumeration with free routines for manipulation, and embedding the enumeration and operations into an enumeration class.

\begin{figure}
\centering
\begin{tabular}{@{}l|l@{}}
\multicolumn{1}{@{}c|}{non-object oriented} & \multicolumn{1}{c@{}}{object oriented} \\
\hline
\begin{Java}
enum Weekday !{!
	Mon, Tue, Wed, Thu, Fri, Sat, Sun !}!;

static boolean isWeekday( Weekday wd ) {
	return wd.ordinal() <= Weekday.Fri.ordinal();
}
static boolean isWeekend( Weekday wd ) {
	return Weekday.Fri.ordinal() < wd.ordinal();
}

public static void main( String[] args ) {
	Weekday day = Weekday.Sat;
	System.out.println( isWeekday( day ) );
	System.out.println( isWeekend( day ) );
}
\end{Java}
&
\begin{Java}
enum Weekday !{!
	Mon, Tue, Wed, Thu, Fri, Sat, Sun;

	public boolean isWeekday() {
		return ordinal() <= Weekday.Fri.ordinal();
	}
	public boolean isWeekend() {
		return Weekday.Fri.ordinal() < ordinal();
	}
!}!
public static void main( String[] args ) {
	WeekDay day = WeekDay.Sat;
	System.out.println( day.isWeekday() );
	System.out.println( day.isWeekend() );
}
\end{Java}
\end{tabular}
\caption{Java: Free Routine Versus Class Enumeration}
\label{f:JavaFreeVersusClass}
\end{figure}

To explicitly assign enumerator values and/or use a non-@int@ enumeration type (any Java type may be used), the enumeration must specify an explicit type in the enumeration class and a constructor.
\begin{Java}
enum Weekday {
	Mon!(1)!, Tue!(2)!, Wed!(3)!, Thu!(4)!, Fri!(5)!, Sat!(6)!, Sun!(7)!; // must appear first
	private !long! day;					$\C{// underlying enumeration type}$
	private Weekday( !long! d ) { day = d; } $\C{// used to initialize enumerators}$
};
Weekday day = Weekday.Sat;
\end{Java}
If an enumerator initialization is a runtime expression, the expression is executed once at the point the enumeration is declaraed.

The position, value, and label are accessible.
\begin{Java}
System.out.println( !day.ordinal()! + " " + !day.day! + " " + !day.name()! );  // 5 6 Sat
\end{Java}
The constructor is private so only initialization or assignment can be used to set an enumeration, which ensures only corresponding enumerator values are allowed.

An enumeration can appear in a @switch@ statement, but no ranges.
\begin{Java}
switch ( day ) {
  case Mon: case Tue: case Wed: case Thu: case Fri:
	System.out.println( "weekday" );
	break;
  case Sat: case Sun:
	System.out.println( "weekend" );
	break;
}
\end{Java}
Like \Csharp, looping over an enumeration is done using method @values@, which returns the array of enumerator values (expensive operation).
\begin{Java}
for ( Weekday iday : Weekday.values() ) {
	System.out.print( iday.ordinal() + iday.day + " " +  iday.name() + ",  " );
}
0 1 Mon,  1 2 Tue,  2 3 Wed,  3 4 Thu,  4 5 Fri,  5 6 Sat,  6 7 Sun,  
\end{Java}

As well, Java provides an @EnumSet@ where the underlying type is an efficient set of bits, one per enumeration \see{\Csharp \lstinline{Flags}, \VRef{s:Csharp}}, providing (logical) operations on groups of enumerators.
There is also a specialized version of @HashMap@ with enumerator keys, which has performance benefits.

Enumeration inheritence is disallowed because an enumeration is @final@.



\section{Modula-3}



\section{Rust}
% https://doc.rust-lang.org/reference/items/enumerations.html

Rust provides a scoped enumeration based on variant types.
% An enumeration, also referred to as an enum, is a simultaneous definition of a nominal enumerated type as well as a set of constructors, that can be used to create or pattern-match values of the corresponding enumerated type.
An enumeration without constructors is called field-less.
\begin{rust}
enum Week { Mon, Tues, Wed, Thu, Fri, Sat, Sun@,@ }
let mut week: Week = Week::Mon;
week = Week::Fri;
\end{rust}
A field-less enumeration with only unit variants is called unit-only.
\begin{rust}
enum Week { Mon = 0, Tues = 1, Wed = 2, Thu = 3, Fri = 4, Sat = 5, Sun = 6 }
\end{rust}
Enum constructors can have either named or unnamed fields:
\begin{rust}
enum Animal {
	Dog( String, f64 ),
	Cat{ name: String, weight: f64 },
}
let mut a: Animal = Animal::Dog("Cocoa".to_string(), 37.2);
a = Animal::Cat { name: "Spotty".to_string(), weight: 2.7 };
\end{rust}
Here, @Dog@ is an @enum@ variant, whereas @Cat@ is a struct-like variant.

Each @enum@ type has an implicit integer tag (discriminant), with a unique value for each variant type.
Like a C enumeration, the tag values for the variant types start at 0 with auto incrementing.
The tag is re-purposed for enumeration by allowing it to be explicitly set, and auto incrmenting continues from that value.
\begin{cquote}
\sf\setlength{\tabcolsep}{3pt}
\begin{tabular}{rcccccccr}
@enum@ Week \{	& Mon,	& Tue,	& Wed = 2,	& Thu = 10,	& Fri, 	& Sat = 5,	& Sun	& \};	\\
\rm tags		& 0		& 1		& 2			& 10		& 11 	& 5			& 6		&		\\
\end{tabular}
\end{cquote}
In general, the tag can only be read as an opaque reference for comparison.
\begin{rust}
if mem::discriminant(&week) == mem::discriminant(&Week::Mon) ...
\end{rust}
If the enumeration is unit-only, or field-less with no explicit discriminants and where only unit variants are explicit, then the discriminant is accessible with a numeric cast.
\begin{rust}
if week as isize == Week::Mon as isize ...
\end{rust}


\section{Swift}

% https://www.programiz.com/swift/online-compiler

A Swift enumeration provides a heterogenous set of enumerators, like a tagged @union@, where the field name is the enumerator and its list of type parameters form its type.
\begin{swift}
enum Many {
	case Mon, Tue, Wed, Thu, Fri, Sat, Sun // basic enumerator
	case code( String ) // string enumerator
	case tuple( Int, Int, Int ) // tuple enumerator
};
var day = Many.Sat; // qualification to resolve type
print( day );
day = .Wed // no qualification after type resolved
print( day );
day = .code( "ABC" );
print( day );
day = .tuple( 1, 2, 3 );
print( day );

Sat
Wed
code("ABC")
tuple(1, 2, 3)
\end{swift}


An enumeration defines a common type for a group of related values and enables you to work with those values in a type-safe way within your code.

If you are familiar with C, you will know that C enumerations assign related names to a set of integer values.
Enumerations in Swift are much more flexible, and don't have to provide a value for each case of the enumeration.
If a value (known as a raw value) is provided for each enumeration case, the value can be a string, a character, or a value of any integer or floating-point type.

Alternatively, enumeration cases can specify associated values of any type to be stored along with each different case value, much as unions or variants do in other languages.
You can define a common set of related cases as part of one enumeration, each of which has a different set of values of appropriate types associated with it.

Enumerations in Swift are first-class types in their own right.
They adopt many features traditionally supported only by classes, such as computed properties to provide additional information about the enumeration's current value, and instance methods to provide functionality related to the values the enumeration represents.
Enumerations can also define initializers to provide an initial case value;
can be extended to expand their functionality beyond their original implementation; and can conform to protocols to provide standard functionality.

For more about these capabilities, see Properties, Methods, Initialization, Extensions, and Protocols.

\paragraph{Enumeration Syntax}


Note:
Swift enumeration cases don't have an integer value set by default, unlike languages like C and Objective-C.
In the CompassPoint example above, @north@, @south@, @east@ and @west@ don't implicitly equal 0, 1, 2 and 3.
Instead, the different enumeration cases are values in their own right, with an explicitly defined type of CompassPoint.

Multiple cases can appear on a single line, separated by commas:
\begin{swift}
enum Planet {
	case mercury, venus, earth, mars, jupiter, saturn, uranus, neptune
}
\end{swift}
Each enumeration definition defines a new type.
Like other types in Swift, their names (such as @CompassPoint@ and @Planet@) start with a capital letter.
Give enumeration types singular rather than plural names, so that they read as self-evident:
\begin{swift}
var directionToHead = CompassPoint.west
\end{swift}
The type of @directionToHead@ is inferred when it's initialized with one of the possible values of @CompassPoint@.
Once @directionToHead@ is declared as a @CompassPoint@, you can set it to a different @CompassPoint@ value using a shorter dot syntax:
\begin{swift}
directionToHead = .east
\end{swift}
The type of @directionToHead@ is already known, and so you can drop the type when setting its value.
This makes for highly readable code when working with explicitly typed enumeration values.

\paragraph{Matching Enumeration Values with a Switch Statement}

You can match individual enumeration values with a switch statement:
\begin{swift}
directionToHead = .south
switch directionToHead {
case .north:
	print("Lots of planets have a north")
case .south:
	print("Watch out for penguins")
case .east:
	print("Where the sun rises")
case .west:
	print("Where the skies are blue")
}
// Prints "Watch out for penguins"
\end{swift}
You can read this code as:
\begin{quote}
"Consider the value of directionToHead.
In the case where it equals @.north@, print "Lots of planets have a north".
In the case where it equals @.south@, print "Watch out for penguins"."

...and so on.
\end{quote}
As described in Control Flow, a switch statement must be exhaustive when considering an enumeration's cases.
If the case for @.west@ is omitted, this code doesn't compile, because it doesn't consider the complete list of @CompassPoint@ cases.
Requiring exhaustiveness ensures that enumeration cases aren't accidentally omitted.

When it isn't appropriate to provide a case for every enumeration case, you can provide a default case to cover any cases that aren't addressed explicitly:
\begin{swift}
let somePlanet = Planet.earth
switch somePlanet {
case .earth:
	print("Mostly harmless")
default:
	print("Not a safe place for humans")
}
// Prints "Mostly harmless"
\end{swift}

\paragraph{Iterating over Enumeration Cases}

For some enumerations, it's useful to have a collection of all of that enumeration's cases.
You enable this by writing @CaseIterable@ after the enumeration's name.
Swift exposes a collection of all the cases as an allCases property of the enumeration type.
Here's an example:
\begin{swift}
enum Beverage: CaseIterable {
	case coffee, tea, juice
}
let numberOfChoices = Beverage.allCases.count
print("\(numberOfChoices) beverages available")
// Prints "3 beverages available"
\end{swift}
In the example above, you write @Beverage.allCases@ to access a collection that contains all of the cases of the @Beverage@ enumeration.
You can use @allCases@ like any other collection -- the collection's elements are instances of the enumeration type, so in this case they're Beverage values.
The example above counts how many cases there are, and the example below uses a for-in loop to iterate over all the cases.
\begin{swift}
for beverage in Beverage.allCases {
	print(beverage)
}
// coffee
// tea
// juice
\end{swift}
The syntax used in the examples above marks the enumeration as conforming to the @CaseIterable@ protocol.
For information about protocols, see Protocols.

\paragraph{Associated Values}
The examples in the previous section show how the cases of an enumeration are a defined (and typed) value in their own right.
You can set a constant or variable to Planet.earth, and check for this value later.
However, it's sometimes useful to be able to store values of other types alongside these case values.
This additional information is called an associated value, and it varies each time you use that case as a value in your code.

You can define Swift enumerations to store associated values of any given type, and the value types can be different for each case of the enumeration if needed.
Enumerations similar to these are known as discriminated unions, tagged unions, or variants in other programming languages.

For example, suppose an inventory tracking system needs to track products by two different types of barcode.
Some products are labeled with 1D barcodes in UPC format, which uses the numbers 0 to 9.
Each barcode has a number system digit, followed by five manufacturer code digits and five product code digits.
These are followed by a check digit to verify that the code has been scanned correctly:

Other products are labeled with 2D barcodes in QR code format, which can use any ISO 8859-1 character and can encode a string up to 2,953 characters long:

It's convenient for an inventory tracking system to store UPC barcodes as a tuple of four integers, and QR code barcodes as a string of any length.

In Swift, an enumeration to define product barcodes of either type might look like this:
\begin{swift}
enum Barcode {
	case upc(Int, Int, Int, Int)
	case qrCode(String)
}
\end{swift}
This can be read as:
\begin{quote}
"Define an enumeration type called Barcode, which can take either a value of upc with an associated value of type @(Int, Int, Int, Int)@, or a value of @qrCode@ with an associated value of type @String@."
\end{quote}
This definition doesn't provide any actual @Int@ or @String@ values -- it just defines the type of associated values that Barcode constants and variables can store when they're equal to @Barcode.upc@ or @Barcode.qrCode@.

You can then create new barcodes using either type:
\begin{swift}
var productBarcode = Barcode.upc(8, 85909, 51226, 3)
\end{swift}
This example creates a new variable called @productBarcode@ and assigns it a value of @Barcode.upc@ with an associated tuple value of @(8, 85909, 51226, 3)@.

You can assign the same product a different type of barcode:
\begin{swift}
productBarcode = .qrCode("ABCDEFGHIJKLMNOP")
\end{swift}
At this point, the original @Barcode.upc@ and its integer values are replaced by the new @Barcode.qrCode@ and its string value.
Constants and variables of type Barcode can store either a @.upc@ or a @.qrCode@ (together with their associated values), but they can store only one of them at any given time.

You can check the different barcode types using a switch statement, similar to the example in Matching Enumeration Values with a Switch Statement.
This time, however, the associated values are extracted as part of the switch statement.
You extract each associated value as a constant (with the let prefix) or a variable (with the var prefix) for use within the switch case's body:
\begin{swift}
switch productBarcode {
case .upc(let numberSystem, let manufacturer, let product, let check):
	print("UPC: \(numberSystem), \(manufacturer), \(product), \(check).")
case .qrCode(let productCode):
	print("QR code: \(productCode).")
}
// Prints "QR code: ABCDEFGHIJKLMNOP."
\end{swift}
If all of the associated values for an enumeration case are extracted as constants, or if all are extracted as variables, you can place a single let or var annotation before the case name, for brevity:
\begin{swift}
switch productBarcode {
case let .upc(numberSystem, manufacturer, product, check):
	print("UPC : \(numberSystem), \(manufacturer), \(product), \(check).")
case let .qrCode(productCode):
	print("QR code: \(productCode).")
}
// Prints "QR code: ABCDEFGHIJKLMNOP."
\end{swift}

\paragraph{Raw Values}

The barcode example in Associated Values shows how cases of an enumeration can declare that they store associated values of different types.
As an alternative to associated values, enumeration cases can come prepopulated with default values (called raw values), which are all of the same type.

Here's an example that stores raw ASCII values alongside named enumeration cases:
\begin{swift}
enum ASCIIControlCharacter: Character {
	case tab = "\t"
	case lineFeed = "\n"
	case carriageReturn = "\r"
}
\end{swift}
Here, the raw values for an enumeration called ASCIIControlCharacter are defined to be of type Character, and are set to some of the more common ASCII control characters.
Character values are described in Strings and Characters.

Raw values can be strings, characters, or any of the integer or floating-point number types.
Each raw value must be unique within its enumeration declaration.

Note

Raw values are not the same as associated values.
Raw values are set to prepopulated values when you first define the enumeration in your code, like the three ASCII codes above.
The raw value for a particular enumeration case is always the same.
Associated values are set when you create a new constant or variable based on one of the enumeration's cases, and can be different each time you do so.
Implicitly Assigned Raw Values

When you're working with enumerations that store integer or string raw values, you don't have to explicitly assign a raw value for each case.
When you don't, Swift automatically assigns the values for you.

For example, when integers are used for raw values, the implicit value for each case is one more than the previous case.
If the first case doesn't have a value set, its value is 0.

The enumeration below is a refinement of the earlier Planet enumeration, with integer raw values to represent each planet's order from the sun:

\begin{swift}
enum Planet: Int {
	case mercury = 1, venus, earth, mars, jupiter, saturn, uranus, neptune
}
\end{swift}
In the example above, Planet.mercury has an explicit raw value of 1, Planet.venus has an implicit raw value of 2, and so on.

When strings are used for raw values, the implicit value for each case is the text of that case's name.

The enumeration below is a refinement of the earlier CompassPoint enumeration, with string raw values to represent each direction's name:
\begin{swift}
enum CompassPoint: String {
	case north, south, east, west
}
\end{swift}
In the example above, CompassPoint.south has an implicit raw value of "south", and so on.

You access the raw value of an enumeration case with its rawValue property:
\begin{swift}
let earthsOrder = Planet.earth.rawValue
// earthsOrder is 3

let sunsetDirection = CompassPoint.west.rawValue
// sunsetDirection is "west"
\end{swift}

\paragraph{Initializing from a Raw Value}

If you define an enumeration with a raw-value type, the enumeration automatically receives an initializer that takes a value of the raw value's type (as a parameter called rawValue) and returns either an enumeration case or nil.
You can use this initializer to try to create a new instance of the enumeration.

This example identifies Uranus from its raw value of 7:
\begin{swift}
let possiblePlanet = Planet(rawValue: 7)
// possiblePlanet is of type Planet? and equals Planet.uranus
\end{swift}
Not all possible Int values will find a matching planet, however.
Because of this, the raw value initializer always returns an optional enumeration case.
In the example above, possiblePlanet is of type Planet?, or "optional Planet."
Note

The raw value initializer is a failable initializer, because not every raw value will return an enumeration case.
For more information, see Failable Initializers.

If you try to find a planet with a position of 11, the optional Planet value returned by the raw value initializer will be nil:
\begin{swift}
let positionToFind = 11
if let somePlanet = Planet(rawValue: positionToFind) {
	switch somePlanet {
	case .earth:
		print("Mostly harmless")
	default:
		print("Not a safe place for humans")
	}
} else {
	print("There isn't a planet at position \(positionToFind)")
}
// Prints "There isn't a planet at position 11"
\end{swift}
This example uses optional binding to try to access a planet with a raw value of 11.
The statement if let somePlanet = Planet(rawValue: 11) creates an optional Planet, and sets somePlanet to the value of that optional Planet if it can be retrieved.
In this case, it isn't possible to retrieve a planet with a position of 11, and so the else branch is executed instead.

\paragraph{Recursive Enumerations}

A recursive enumeration is an enumeration that has another instance of the enumeration as the associated value for one or more of the enumeration cases.
You indicate that an enumeration case is recursive by writing indirect before it, which tells the compiler to insert the necessary layer of indirection.

For example, here is an enumeration that stores simple arithmetic expressions:
\begin{swift}
enum ArithmeticExpression {
	case number(Int)
	indirect case addition(ArithmeticExpression, ArithmeticExpression)
	indirect case multiplication(ArithmeticExpression, ArithmeticExpression)
}
\end{swift}
You can also write indirect before the beginning of the enumeration to enable indirection for all of the enumeration's cases that have an associated value:
\begin{swift}
indirect enum ArithmeticExpression {
	case number(Int)
	case addition(ArithmeticExpression, ArithmeticExpression)
	case multiplication(ArithmeticExpression, ArithmeticExpression)
}
\end{swift}
This enumeration can store three kinds of arithmetic expressions: a plain number, the addition of two expressions, and the multiplication of two expressions.
The addition and multiplication cases have associated values that are also arithmetic expressions -- these associated values make it possible to nest expressions.
For example, the expression (5 + 4) * 2 has a number on the right-hand side of the multiplication and another expression on the left-hand side of the multiplication.
Because the data is nested, the enumeration used to store the data also needs to support nesting -- this means the enumeration needs to be recursive.
The code below shows the ArithmeticExpression recursive enumeration being created for (5 + 4) * 2:
\begin{swift}
let five = ArithmeticExpression.number(5)
let four = ArithmeticExpression.number(4)
let sum = ArithmeticExpression.addition(five, four)
let product = ArithmeticExpression.multiplication(sum, ArithmeticExpression.number(2))
\end{swift}
A recursive function is a straightforward way to work with data that has a recursive structure.
For example, here's a function that evaluates an arithmetic expression:
\begin{swift}
func evaluate(_ expression: ArithmeticExpression) -> Int {
	switch expression {
	case let .number(value):
		return value
	case let .addition(left, right):
		return evaluate(left) + evaluate(right)
	case let .multiplication(left, right):
		return evaluate(left) * evaluate(right)
	}
}

print(evaluate(product))
// Prints "18"
\end{swift}
This function evaluates a plain number by simply returning the associated value.
It evaluates an addition or multiplication by evaluating the expression on the left-hand side, evaluating the expression on the right-hand side, and then adding them or multiplying them.


\section{Python 3.13}
% https://docs.python.org/3/howto/enum.html

Python is a dynamically-typed reflexive programming language with multiple versions, and hence, it is possible to extend existing or build new language features within the language.
As a result, discussing Python enumerations is a moving target, because if a features does not exist, if can often be created with varying levels of complexity.
Nevertheless, an attempt has been made to discuss core enumeration features that come with Python 3.13.

A Python enumeration type is a set of ordered scoped identifiers (enumerators) bound to \emph{unique} values.
An enumeration is not a basic type;
it is a @class@ inheriting from the @Enum@ class, where the enumerators must be explicitly initialized, \eg:
\begin{python}
class Week(@Enum@): Mon = 1; Tue = 2; Wed = 3; Thu = 4; Fri = 5; Sat = 6; Sun = 7
\end{python}
and/or explicitly auto initialized, \eg:
\begin{python}
class Week(Enum): Mon = 1; Tue = 2; Wed = 3; Thu = 10; Fri = @auto()@; Sat = 4; Sun = @auto()@
\end{python}
where @auto@ increments by 1 from the previous enumerator value.
Object initialization and assignment are restricted to the enumerators of this type.
An enumerator initialized with same value is an alias and invisible at the enumeration level, \ie the alias it substituted for its aliasee.
\begin{python}
class Week(Enum): Mon = 1; Tue = 2; Wed = 3; Thu = 10; Fri = @10@; Sat = @10@; Sun = @10@
\end{python}
Here, the enumeration has only 4 enumerators and 3 aliases.
An alias is only visible by dropping down to the @class@ level and asking for class members.
@Enum@ only supports equality comparison between enumerator values;
the extended class @OrderedEnum@ adds relational operators @<@, @<=@, @>@, and @>=@.

There are bidirectional enumeration pseudo-functions for label and value, but there is no concept of access using ordering (position).
\begin{cquote}
\setlength{\tabcolsep}{15pt}
\begin{tabular}{@{}ll@{}}
\begin{python}
Week.Thu.value == 10;
Week.Thu.name == 'Thu';
\end{python}
&
\begin{python}
Week( 10 ) == Thu
Week['Thu'].value = 10
\end{python}
\end{tabular}
\end{cquote}

As an enumeration is a \lstinline[language=python]{class}, its own methods.
\begin{python}
class Week(Enum):
	Mon = 1; Tue = 2; Wed = 3; Thu = 4; Fri = 5; Sat = 6; Sun = 7
	$\\@$classmethod
	def today(cls, date):
		return cls(date.isoweekday())
print( "today:", Week.today(date.today()))
today: Week.Mon
\end{python}
The method @today@ retrieves the day of the week and uses it as an index to print out the corresponding label of @Week@.

@Flag@ allows combining several members into a single variable:
\begin{python}
print( repr(WeekF.Sat | WeekF.Sun) )
<WeekF.Sun|Sat: 96>
\end{python}
You can even iterate over a @Flag@ variable:
\begin{python}
for day in weekend:
	print(day)
WeekF.Sat
WeekF.Sun
\end{python}
Okay, let's get some chores set up:
\begin{python}
>>> chores_for_ethan = {
...    'feed the cat': Week.MONDAY | Week.WEDNESDAY | Week.FRIDAY,
...    'do the dishes': Week.TUESDAY | Week.THURSDAY,
...    'answer SO questions': Week.SATURDAY,
...    }
\end{python}
And a function to display the chores for a given day:
\begin{python}
>>> def show_chores(chores, day):
...    for chore, days in chores.items():
...        if day in days:
...            print(chore)
>>> show_chores(chores_for_ethan, Week.SATURDAY)
answer SO questions
\end{python}
Auto incrmenet for @Flag@ is by powers of 2.
\begin{python}
class WeekF(Flag): Mon = auto(); Tue = auto(); Wed = auto(); Thu = auto(); Fri = auto();  \
							Sat = auto(); Sun = auto(); Weekend = Sat | Sun
for d in WeekF:
	print( f"{d.name}: {d.value}", end=" ")
Mon: 1 Tue: 2 Wed: 4 Thu: 8 Fri: 16 Sat: 32 Sun: 64 WeekA.Weekend
\end{python}

\subsection{Programmatic access to enumeration members and their attributes}

Sometimes it's useful to access members in enumerations programmatically (i.e. situations where @Color.RED@ won't do because the exact color is not known at program-writing time).
@Enum@ allows such access:
\begin{python}
print(RGB(1), RGB(3), )
RGB.RED RGB.GREEN
\end{python}
If you want to access enum members by name, use item access:
\begin{python}
print( RGBa['RED'], RGBa['GREEN'] )
RGB.RED RGB.GREEN
\end{python}
If you have an enum member and need its name or value:
\begin{python}
member = RGBa.RED
print( f"{member.name} {member.value}" )
RED 1
\end{python}


\subsection{Ensuring unique enumeration values}

By default, enumerations allow multiple names as aliases for the same value.
When this behavior isn't desired, you can use the @unique()@ decorator:
\begin{python}
from enum import Enum, unique
$@$unique
class DupVal(Enum): ONE = 1; TWO = 2; THREE = 3; FOUR = 3
ValueError: duplicate values found in <enum 'Mistake'>: FOUR -> THREE
\end{python}

\subsection{Using automatic values}

If the exact value is unimportant you can use @auto@:
\begin{python}
from enum import Enum, auto
class RGBa(Enum): RED = auto(); BLUE = auto(); GREEN = auto()
\end{python}
(Like Golang @iota@.)
The values are chosen by @_generate_next_value_()@, which can be overridden:
\begin{python}
>>> class AutoName(Enum):
...     $@$staticmethod
...     def _generate_next_value_(name, start, count, last_values):
...         return name
... 
>>> class Ordinal(AutoName):
...     NORTH = auto()
...     SOUTH = auto()
...     EAST = auto()
...     WEST = auto()
... 
>>> [member.value for member in Ordinal]
['NORTH', 'SOUTH', 'EAST', 'WEST']
\end{python}
Note The @_generate_next_value_()@ method must be defined before any members.

\subsection{Iteration}

Iterating over the members of an enum does not provide the aliases:
\begin{python}
>>> list(Shape)
[<Shape.SQUARE: 2>, <Shape.DIAMOND: 1>, <Shape.CIRCLE: 3>]
>>> list(Week)
[<Week.MONDAY: 1>, <Week.TUESDAY: 2>, <Week.WEDNESDAY: 4>, <Week.THURSDAY: 8>,
<Week.FRIDAY: 16>, <Week.SATURDAY: 32>, <Week.SUNDAY: 64>]
\end{python}
Note that the aliases @Shape.ALIAS_FOR_SQUARE@ and @Week.WEEKEND@ aren't shown.

The special attribute @__members__@ is a read-only ordered mapping of names to members.
It includes all names defined in the enumeration, including the aliases:
\begin{python}
>>> for name, member in Shape.__members__.items():
...     name, member
... 
('SQUARE', <Shape.SQUARE: 2>)
('DIAMOND', <Shape.DIAMOND: 1>)
('CIRCLE', <Shape.CIRCLE: 3>)
('ALIAS_FOR_SQUARE', <Shape.SQUARE: 2>)
\end{python}
The @__members__@ attribute can be used for detailed programmatic access to the enumeration members.
For example, finding all the aliases:
\begin{python}
>>> [name for name, member in Shape.__members__.items() if member.name != name]
['ALIAS_FOR_SQUARE']
\end{python}
Note: Aliases for flags include values with multiple flags set, such as 3, and no flags set, i.e. 0.

\subsection{Comparisons}

Enumeration members are compared by identity:
\begin{python}
>>> Color.RED is Color.RED
True
>>> Color.RED is Color.BLUE
False
>>> Color.RED is not Color.BLUE
True
\end{python}
Ordered comparisons between enumeration values are not supported.
Enum members are not integers (but see @IntEnum@ below):
\begin{python}
>>> Color.RED < Color.BLUE
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: '<' not supported between instances of 'Color' and 'Color'
\end{python}
Equality comparisons are defined though:
\begin{python}
>>> Color.BLUE == Color.RED
False
>>> Color.BLUE != Color.RED
True
>>> Color.BLUE == Color.BLUE
True
\end{python}
Comparisons against non-enumeration values will always compare not equal (again, @IntEnum@ was explicitly designed to behave differently, see below):
\begin{python}
>>> Color.BLUE == 2
False
\end{python}

Warning: It is possible to reload modules -- if a reloaded module contains enums, they will be recreated, and the new members may not compare identical/equal to the original members.

\subsection{Allowed members and attributes of enumerations}

Most of the examples above use integers for enumeration values.
Using integers is short and handy (and provided by default by the Functional API), but not strictly enforced.
In the vast majority of use-cases, one doesn't care what the actual value of an enumeration is.
But if the value is important, enumerations can have arbitrary values.

Enumerations are Python classes, and can have methods and special methods as usual. If we have this enumeration:
\begin{python}
>>> class Mood(Enum):
...     FUNKY = 1
...     HAPPY = 3
... 
...     def describe(self):
...         # self is the member here
...         return self.name, self.value
... 
...     def __str__(self):
...         return 'my custom str! {0}'.format(self.value)
... 
...     $@$classmethod
... 
...     def favorite_mood(cls):
...         # cls here is the enumeration
...         return cls.HAPPY
...
\end{python}
Then:
\begin{python}
>>> Mood.favorite_mood()
<Mood.HAPPY: 3>
>>> Mood.HAPPY.describe()
('HAPPY', 3)
>>> str(Mood.FUNKY)
'my custom str! 1'
\end{python}
The rules for what is allowed are as follows: names that start and end with a single underscore are reserved by enum and cannot be used;
all other attributes defined within an enumeration will become members of this enumeration, with the exception of special methods (@__str__()@, @__add__()@, etc.), descriptors (methods are also descriptors), and variable names listed in @_ignore_@.

Note: if your enumeration defines @__new__()@ and/or @__init__()@, any value(s) given to the enum member will be passed into those methods.
See Planet for an example.

Note: The @__new__()@ method, if defined, is used during creation of the Enum members;
it is then replaced by Enum's @__new__()@ which is used after class creation for lookup of existing members.
See When to use @__new__()@ vs. @__init__()@ for more details.

\subsection{Restricted Enum subclassing}

A new @Enum@ class must have one base enum class, up to one concrete data type, and as many object-based mixin classes as needed.
The order of these base classes is:
\begin{python}
class EnumName([mix-in, ...,] [data-type,] base-enum):
	pass
\end{python}
Also, subclassing an enumeration is allowed only if the enumeration does not define any members.
So this is forbidden:
\begin{python}
>>> class MoreColor(Color):
...     PINK = 17
... 
Traceback (most recent call last):
...
TypeError: <enum 'MoreColor'> cannot extend <enum 'Color'>
\end{python}
But this is allowed:
\begin{python}
>>> class Foo(Enum):
...     def some_behavior(self):
...         pass
... 
>>> class Bar(Foo):
...     HAPPY = 1
...     SAD = 2
... 
\end{python}
Allowing subclassing of enums that define members would lead to a violation of some important invariants of types and instances.
On the other hand, it makes sense to allow sharing some common behavior between a group of enumerations. (See OrderedEnum for an example.)

\subsection{Dataclass support}

When inheriting from a @dataclass@, the @__repr__()@ omits the inherited class' name.
For example:
\begin{python}
>>> from dataclasses import dataclass, field
>>> $@$dataclass
... class CreatureDataMixin:
...     size: str
...     legs: int
...     tail: bool = field(repr=False, default=True)
... 
>>> class Creature(CreatureDataMixin, Enum):
...     BEETLE = 'small', 6
...     DOG = 'medium', 4
... 
>>> Creature.DOG
<Creature.DOG: size='medium', legs=4>
\end{python}
Use the @dataclass()@ argument repr=False to use the standard @repr()@.

Changed in version 3.12: Only the dataclass fields are shown in the value area, not the dataclass' name.

\subsection{Pickling}

Enumerations can be pickled and unpickled:
\begin{python}
>>> from test.test_enum import Fruit
>>> from pickle import dumps, loads
>>> Fruit.TOMATO is loads(dumps(Fruit.TOMATO))
True
\end{python}
The usual restrictions for pickling apply: picklable enums must be defined in the top level of a module, since unpickling requires them to be importable from that module.

Note: With pickle protocol version 4 it is possible to easily pickle enums nested in other classes.

It is possible to modify how enum members are pickled/unpickled by defining @__reduce_ex__()@ in the enumeration class.
The default method is by-value, but enums with complicated values may want to use by-name:
\begin{python}
>>> import enum
>>> class MyEnum(enum.Enum):
...     __reduce_ex__ = enum.pickle_by_enum_name
\end{python}
Note: Using by-name for flags is not recommended, as unnamed aliases will not unpickle.

\subsection{Functional API}

The @Enum@ class is callable, providing the following functional API:
\begin{python}
>>> Animal = Enum('Animal', 'ANT BEE CAT DOG')
>>> Animal
<enum 'Animal'>
>>> Animal.ANT
<Animal.ANT: 1>
>>> list(Animal)
[<Animal.ANT: 1>, <Animal.BEE: 2>, <Animal.CAT: 3>, <Animal.DOG: 4>]
\end{python}
The semantics of this API resemble @namedtuple@.
The first argument of the call to @Enum@ is the name of the enumeration.

The second argument is the source of enumeration member names.
It can be a whitespace-separated string of names, a sequence of names, a sequence of 2-tuples with key/value pairs, or a mapping (e.g. dictionary) of names to values.
The last two options enable assigning arbitrary values to enumerations;
the others auto-assign increasing integers starting with 1 (use the @start@ parameter to specify a different starting value).
A new class derived from @Enum@ is returned.
In other words, the above assignment to Animal is equivalent to:
\begin{python}
>>> class Animal(Enum):
...     ANT = 1
...     BEE = 2
...     CAT = 3
...     DOG = 4
... 
\end{python}
The reason for defaulting to 1 as the starting number and not 0 is that 0 is @False@ in a boolean sense, but by default enum members all evaluate to @True@.

Pickling enums created with the functional API can be tricky as frame stack implementation details are used to try and figure out which module the enumeration is being created in (e.g. it will fail if you use a utility function in a separate module, and also may not work on IronPython or Jython).
The solution is to specify the module name explicitly as follows:
\begin{python}
>>> Animal = Enum('Animal', 'ANT BEE CAT DOG', module=__name__)
\end{python}
Warning: If module is not supplied, and @Enum@ cannot determine what it is, the new @Enum@ members will not be unpicklable; to keep errors closer to the source, pickling will be disabled.

The new pickle protocol 4 also, in some circumstances, relies on @__qualname__@ being set to the location where pickle will be able to find the class.
For example, if the class was made available in class SomeData in the global scope:
\begin{python}
>>> Animal = Enum('Animal', 'ANT BEE CAT DOG', qualname='SomeData.Animal')
\end{python}
The complete signature is:
\begin{python}
Enum(
	value='NewEnumName',
	names=<...>,
	*,
	module='...',
	qualname='...',
	type=<mixed-in class>,
	start=1,
	)
\end{python}
\begin{itemize}
\item
@value@: What the new enum class will record as its name.
\item
@names@: The enum members.
This can be a whitespace- or comma-separated string (values will start at 1 unless otherwise specified):
\begin{python}
'RED GREEN BLUE' | 'RED,GREEN,BLUE' | 'RED, GREEN, BLUE'
\end{python}
or an iterator of names:
\begin{python}
['RED', 'GREEN', 'BLUE']
\end{python}
or an iterator of (name, value) pairs:
\begin{python}
[('CYAN', 4), ('MAGENTA', 5), ('YELLOW', 6)]
\end{python}
or a mapping:
\begin{python}
{'CHARTREUSE': 7, 'SEA_GREEN': 11, 'ROSEMARY': 42}
\end{python}
\item
module: name of module where new enum class can be found.
\item
@qualname@: where in module new enum class can be found.
\item
@type@: type to mix in to new enum class.
\item
@start@: number to start counting at if only names are passed in.
\end{itemize}
Changed in version 3.5: The start parameter was added.

\subsection{Derived Enumerations}

\subsection{IntEnum}

The first variation of @Enum@ that is provided is also a subclass of @int@.
Members of an @IntEnum@ can be compared to integers;
by extension, integer enumerations of different types can also be compared to each other:
\begin{python}
>>> from enum import IntEnum
>>> class Shape(IntEnum):
...     CIRCLE = 1
...     SQUARE = 2
... 
>>> class Request(IntEnum):
...     POST = 1
...     GET = 2
... 
>>> Shape == 1
False
>>> Shape.CIRCLE == 1
True
>>> Shape.CIRCLE == Request.POST
True
\end{python}
However, they still can't be compared to standard @Enum@ enumerations:
\begin{python}
>>> class Shape(IntEnum):
...     CIRCLE = 1
...     SQUARE = 2
... 
>>> class Color(Enum):
...     RED = 1
...     GREEN = 2
... 
>>> Shape.CIRCLE == Color.RED
False
\end{python}
@IntEnum@ values behave like integers in other ways you'd expect:
\begin{python}
>>> int(Shape.CIRCLE)
1
>>> ['a', 'b', 'c'][Shape.CIRCLE]
'b'
>>> [i for i in range(Shape.SQUARE)]
[0, 1]
\end{python}

\subsection{StrEnum}

The second variation of @Enum@ that is provided is also a subclass of @str@.
Members of a @StrEnum@ can be compared to strings;
by extension, string enumerations of different types can also be compared to each other.

New in version 3.11.

\subsection{IntFlag}

The next variation of @Enum@ provided, @IntFlag@, is also based on @int@.
The difference being @IntFlag@ members can be combined using the bitwise operators (@&, |, ^, ~@) and the result is still an @IntFlag@ member, if possible.
Like @IntEnum@, @IntFlag@ members are also integers and can be used wherever an int is used.

Note: Any operation on an IntFlag member besides the bit-wise operations will lose the @IntFlag@ membership.

Bit-wise operations that result in invalid @IntFlag@ values will lose the @IntFlag@ membership.
See @FlagBoundary@ for details.

New in version 3.6.

Changed in version 3.11.

Sample @IntFlag@ class:
\begin{python}
>>> from enum import IntFlag
>>> class Perm(IntFlag):
...     R = 4
...     W = 2
...     X = 1
... 
>>> Perm.R | Perm.W
<Perm.R|W: 6>
>>> Perm.R + Perm.W
6
>>> RW = Perm.R | Perm.W
>>> Perm.R in RW
True
\end{python}
It is also possible to name the combinations:
\begin{python}
>>> class Perm(IntFlag):
...     R = 4
...     W = 2
...     X = 1
...     RWX = 7
... 
>>> Perm.RWX
<Perm.RWX: 7>
>>> ~Perm.RWX
<Perm: 0>
>>> Perm(7)
<Perm.RWX: 7>
\end{python}
Note: Named combinations are considered aliases. Aliases do not show up during iteration, but can be returned from by-value lookups.

Changed in version 3.11.

Another important difference between @IntFlag@ and @Enum@ is that if no flags are set (the value is 0), its boolean evaluation is @False@:
\begin{python}
>>> Perm.R & Perm.X
<Perm: 0>
>>> bool(Perm.R & Perm.X)
False
\end{python}
Because @IntFlag@ members are also subclasses of int they can be combined with them (but may lose @IntFlag@ membership:
\begin{python}
>>> Perm.X | 4
<Perm.R|X: 5>

>>> Perm.X + 8
9
\end{python}
Note: The negation operator, @~@, always returns an @IntFlag@ member with a positive value:
\begin{python}
>>> (~Perm.X).value == (Perm.R|Perm.W).value == 6
True
\end{python}
@IntFlag@ members can also be iterated over:
\begin{python}
>>> list(RW)
[<Perm.R: 4>, <Perm.W: 2>]
\end{python}
New in version 3.11.

\subsection{Flag}

The last variation is @Flag@.
Like @IntFlag@, @Flag@ members can be combined using the bitwise operators (@&, |, ^, ~@).
Unlike @IntFlag@, they cannot be combined with, nor compared against, any other @Flag@ enumeration, nor @int@.
While it is possible to specify the values directly it is recommended to use @auto@ as the value and let @Flag@ select an appropriate value.

New in version 3.6.

Like @IntFlag@, if a combination of @Flag@ members results in no flags being set, the boolean evaluation is @False@:
\begin{python}
>>> from enum import Flag, auto
>>> class Color(Flag):
...     RED = auto()
...     BLUE = auto()
...     GREEN = auto()
... 
>>> Color.RED & Color.GREEN
<Color: 0>
>>> bool(Color.RED & Color.GREEN)
False
\end{python}
Individual flags should have values that are powers of two (1, 2, 4, 8, ...), while combinations of flags will not:
\begin{python}
>>> class Color(Flag):
...     RED = auto()
...     BLUE = auto()
...     GREEN = auto()
...     WHITE = RED | BLUE | GREEN
... 
>>> Color.WHITE
<Color.WHITE: 7>
\end{python}
Giving a name to the ``no flags set'' condition does not change its boolean value:
\begin{python}
>>> class Color(Flag):
...     BLACK = 0
...     RED = auto()
...     BLUE = auto()
...     GREEN = auto()
... 
>>> Color.BLACK
<Color.BLACK: 0>
>>> bool(Color.BLACK)
False
\end{python}
@Flag@ members can also be iterated over:
\begin{python}
>>> purple = Color.RED | Color.BLUE
>>> list(purple)
[<Color.RED: 1>, <Color.BLUE: 2>]
\end{python}
New in version 3.11.

Note: For the majority of new code, @Enum@ and @Flag@ are strongly recommended, since @IntEnum@ and @IntFlag@ break some semantic promises of an enumeration (by being comparable to integers, and thus by transitivity to other unrelated enumerations).
@IntEnum@ and @IntFlag@ should be used only in cases where @Enum@ and @Flag@ will not do;
for example, when integer constants are replaced with enumerations, or for interoperability with other systems.

\subsection{Others}

While @IntEnum@ is part of the enum module, it would be very simple to implement independently:
\begin{python}
class IntEnum(int, Enum):
	pass
\end{python}
This demonstrates how similar derived enumerations can be defined;
for example a @FloatEnum@ that mixes in float instead of @int@.

Some rules:
\begin{itemize}
\item
When subclassing @Enum@, mix-in types must appear before @Enum@ itself in the sequence of bases, as in the @IntEnum@ example above.
\item
Mix-in types must be subclassable.
For example, @bool@ and @range@ are not subclassable and will throw an error during Enum creation if used as the mix-in type.
\item
While @Enum@ can have members of any type, once you mix in an additional type, all the members must have values of that type, e.g. @int@ above.
This restriction does not apply to mix-ins which only add methods and don't specify another type.
\item
When another data type is mixed in, the value attribute is not the same as the enum member itself, although it is equivalent and will compare equal.
\item
A data type is a mixin that defines @__new__()@, or a @dataclass@
\item
\%-style formatting: @%s@ and @%r@ call the @Enum@ class's @__str__()@ and @__repr__()@ respectively; other codes (such as @%i@ or @%h@ for @IntEnum@) treat the enum member as its mixed-in type.
\item
Formatted string literals, @str.format()@, and format() will use the enum's @__str__()@ method.
\end{itemize}
Note: Because @IntEnum@, @IntFlag@, and @StrEnum@ are designed to be drop-in replacements for existing constants, their @__str__()@ method has been reset to their data types' @__str__()@ method.

\subsection{When to use \lstinline{__new__()} vs. \lstinline{__init__()}}

@__new__()@ must be used whenever you want to customize the actual value of the @Enum@ member.
Any other modifications may go in either @__new__()@ or @__init__()@, with @__init__()@ being preferred.

For example, if you want to pass several items to the constructor, but only want one of them to be the value:
\begin{python}
>>> class Coordinate(bytes, Enum):
...     """
...     Coordinate with binary codes that can be indexed by the int code.
...     """
...     def __new__(cls, value, label, unit):
...         obj = bytes.__new__(cls, [value])
...         obj._value_ = value
...         obj.label = label
...         obj.unit = unit
...         return obj
...     PX = (0, 'P.X', 'km')
...     PY = (1, 'P.Y', 'km')
...     VX = (2, 'V.X', 'km/s')
...     VY = (3, 'V.Y', 'km/s')

>>> print(Coordinate['PY'])
Coordinate.PY

>>> print(Coordinate(3))
Coordinate.VY
\end{python}
Warning: Do not call @super().__new__()@, as the lookup-only @__new__@ is the one that is found; instead, use the data type directly.

\subsection{Finer Points}

Supported @__dunder__@ names

@__members__@ is a read-only ordered mapping of member\_name:member items. It is only available on the class.

@__new__()@, if specified, must create and return the enum members; it is also a very good idea to set the member's @_value_@ appropriately. Once all the members are created it is no longer used.
Supported @_sunder_@ names
\begin{itemize}
\item
@_name_@ -- name of the member
\item
@_value_@ -- value of the member; can be set / modified in @__new__@
\item
@_missing_@ -- a lookup function used when a value is not found; may be overridden
\item
@_ignore_@ -- a list of names, either as a @list@ or a @str@, that will not be transformed into members, and will be removed from the final class
\item
@_order_@ -- used in Python 2/3 code to ensure member order is consistent (class attribute, removed during class creation)
\item
@_generate_@next@_value_@ -- used by the Functional API and by @auto@ to get an appropriate value for an enum member; may be overridden
\end{itemize}
Note: For standard @Enum@ classes the next value chosen is the last value seen incremented by one.

For @Flag@ classes the next value chosen will be the next highest power-of-two, regardless of the last value seen.

New in version 3.6: @_missing_@, @_order_@, @_generate_@next@_value_@

New in version 3.7: @_ignore_@

To help keep Python 2 / Python 3 code in sync an @_order_@ attribute can be provided.
It will be checked against the actual order of the enumeration and raise an error if the two do not match:
\begin{python}
>>> class Color(Enum):
...     _order_ = 'RED GREEN BLUE'
...     RED = 1
...     BLUE = 3
...     GREEN = 2
... 
Traceback (most recent call last):
...
TypeError: member order does not match _order_:
  ['RED', 'BLUE', 'GREEN']
  ['RED', 'GREEN', 'BLUE']
\end{python}
Note: In Python 2 code the @_order_@ attribute is necessary as definition order is lost before it can be recorded.

\subsection{\lstinline{_Private__names}}

Private names are not converted to enum members, but remain normal attributes.

Changed in version 3.11.

\subsection{\lstinline{Enum} member type}

@Enum@ members are instances of their enum class, and are normally accessed as @EnumClass.member@.
In certain situations, such as writing custom enum behavior, being able to access one member directly from another is useful, and is supported;
however, in order to avoid name clashes between member names and attributes/methods from mixed-in classes, upper-case names are strongly recommended.

Changed in version 3.5.

\subsection{Creating members that are mixed with other data types}

When subclassing other data types, such as @int@ or @str@, with an @Enum@, all values after the = @are@ passed to that data type's constructor. For example:
\begin{python}
>>> class MyEnum(IntEnum):      # help(int) -> int(x, base=10) -> integer
...     example = '11', 16      # so x='11' and base=16
... 
MyEnum.example.value        # and hex(11) is...
17
\end{python}

\subsection{\lstinline{Boolean} value of \lstinline{Enum} classes and members}

Enum classes that are mixed with non-@Enum@ types (such as @int@, @str@, etc.) are evaluated according to the mixed-in type's rules;
otherwise, all members evaluate as @True@.
To make your own enum's boolean evaluation depend on the member's value add the following to your class:
\begin{python}
def __bool__(self):
	return bool(self.value)
\end{python}
Plain @Enum@ classes always evaluate as @True@.

\subsection{\lstinline{Enum} classes with methods}

If you give your enum subclass extra methods, like the Planet class below, those methods will show up in a dir() of the member, but not of the class:
\begin{python}
>>> dir(Planet)                         
['EARTH', 'JUPITER', 'MARS', 'MERCURY', 'NEPTUNE', 'SATURN', 'URANUS', 'VENUS',
 '__class__', '__doc__', '__members__', '__module__']
>>> dir(Planet.EARTH)                   
['__class__', '__doc__', '__module__', 'mass', 'name', 'radius', 'surface_gravity', 'value']
\end{python}

\subsection{Combining members of \lstinline{Flag}}

Iterating over a combination of @Flag@ members will only return the members that are comprised of a single bit:
\begin{python}
>>> class Color(Flag):
...     RED = auto()
...     GREEN = auto()
...     BLUE = auto()
...     MAGENTA = RED | BLUE
...     YELLOW = RED | GREEN
...     CYAN = GREEN | BLUE
... 
>>> Color(3)  # named combination
<Color.YELLOW: 3>
>>> Color(7)      # not named combination
<Color.RED|GREEN|BLUE: 7>
\end{python}

\subsection{\lstinline{Flag} and \lstinline{IntFlag} minutia}

Using the following snippet for our examples:
\begin{python}
>>> class Color(IntFlag):
...     BLACK = 0
...     RED = 1
...     GREEN = 2
...     BLUE = 4
...     PURPLE = RED | BLUE
...     WHITE = RED | GREEN | BLUE
... 
\end{python}
the following are true:
\begin{itemize}
\item
single-bit flags are canonical
\item
multi-bit and zero-bit flags are aliases
\item
only canonical flags are returned during iteration:
\begin{python}
>>> list(Color.WHITE)
[<Color.RED: 1>, <Color.GREEN: 2>, <Color.BLUE: 4>]
\end{python}
negating a flag or flag set returns a new flag/flag set with the corresponding positive integer value:
\begin{python}
>>> Color.BLUE
<Color.BLUE: 4>

>>> ~Color.BLUE
<Color.RED|GREEN: 3>
\end{python}
\item
names of pseudo-flags are constructed from their members' names:
\begin{python}
>>> (Color.RED | Color.GREEN).name
'RED|GREEN'
\end{python}
\item
multi-bit flags, aka aliases, can be returned from operations:
\begin{python}
>>> Color.RED | Color.BLUE
<Color.PURPLE: 5>

>>> Color(7)  # or Color(-1)
<Color.WHITE: 7>

>>> Color(0)
<Color.BLACK: 0>
\end{python}
\item
membership / containment checking: zero-valued flags are always considered to be contained:
\begin{python}
>>> Color.BLACK in Color.WHITE
True
\end{python}
otherwise, only if all bits of one flag are in the other flag will True be returned:
\begin{python}
>>> Color.PURPLE in Color.WHITE
True

>>> Color.GREEN in Color.PURPLE
False
\end{python}
\end{itemize}
There is a new boundary mechanism that controls how out-of-range / invalid bits are handled: @STRICT@, @CONFORM@, @EJECT@, and @KEEP@:
\begin{itemize}
\item
@STRICT@ --> raises an exception when presented with invalid values
\item
@CONFORM@ --> discards any invalid bits
\item
@EJECT@ --> lose Flag status and become a normal int with the given value
\item
@KEEP@ --> keep the extra bits
\begin{itemize}
\item
keeps Flag status and extra bits
\item
extra bits do not show up in iteration
\item
extra bits do show up in repr() and str()
\end{itemize}
\end{itemize}
The default for @Flag@ is @STRICT@, the default for @IntFlag@ is @EJECT@, and the default for @_convert_@ is @KEEP@ (see @ssl.Options@ for an example of when @KEEP@ is needed).

\section{How are Enums and Flags different?}

Enums have a custom metaclass that affects many aspects of both derived @Enum@ classes and their instances (members).

\subsection{Enum Classes}

The @EnumType@ metaclass is responsible for providing the @__contains__()@, @__dir__()@, @__iter__()@ and other methods that allow one to do things with an @Enum@ class that fail on a typical class, such as @list(Color)@ or @some_enum_var@ in @Color@.
@EnumType@ is responsible for ensuring that various other methods on the final @Enum@ class are correct (such as @__new__()@, @__getnewargs__()@, @__str__()@ and @__repr__()@).

\subsection{Flag Classes}

Flags have an expanded view of aliasing: to be canonical, the value of a flag needs to be a power-of-two value, and not a duplicate name.
So, in addition to the @Enum@ definition of alias, a flag with no value (a.k.a. 0) or with more than one power-of-two value (e.g. 3) is considered an alias.

\subsection{Enum Members (aka instances)}

The most interesting thing about enum members is that they are singletons.
@EnumType@ creates them all while it is creating the enum class itself, and then puts a custom @__new__()@ in place to ensure that no new ones are ever instantiated by returning only the existing member instances.

\subsection{Flag Members}

Flag members can be iterated over just like the @Flag@ class, and only the canonical members will be returned.
For example:
\begin{python}
>>> list(Color)
[<Color.RED: 1>, <Color.GREEN: 2>, <Color.BLUE: 4>]
\end{python}
(Note that BLACK, PURPLE, and WHITE do not show up.)

Inverting a flag member returns the corresponding positive value, rather than a negative value -- for example:
\begin{python}
>>> ~Color.RED
<Color.GREEN|BLUE: 6>
\end{python}
Flag members have a length corresponding to the number of power-of-two values they contain. For example:
\begin{python}
>>> len(Color.PURPLE)
2
\end{python}

\subsection{Enum Cookbook}

While @Enum@, @IntEnum@, @StrEnum@, @Flag@, and @IntFlag@ are expected to cover the majority of use-cases, they cannot cover them all. Here are recipes for some different types of enumerations that can be used directly, or as examples for creating one's own.

\subsection{Omitting values}

In many use-cases, one doesn't care what the actual value of an enumeration is. There are several ways to define this type of simple enumeration:
\begin{itemize}
\item
use instances of auto for the value
\item
use instances of object as the value
\item
use a descriptive string as the value
\item
use a tuple as the value and a custom @__new__()@ to replace the tuple with an @int@ value
\end{itemize}
Using any of these methods signifies to the user that these values are not important, and also enables one to add, remove, or reorder members without having to renumber the remaining members.

\subsection{Using \lstinline{auto}}

Using @auto@ would look like:
\begin{python}
>>> class Color(Enum):
...     RED = auto()
...     BLUE = auto()
...     GREEN = auto()
... 
>>> Color.GREEN
<Color.GREEN: 3>
\end{python}

\subsection{Using \lstinline{object}}

Using @object@ would look like:
\begin{python}
>>> class Color(Enum):
...     RED = object()
...     GREEN = object()
...     BLUE = object()
... 
>>> Color.GREEN                         
<Color.GREEN: <object object at 0x...>>
\end{python}
This is also a good example of why you might want to write your own @__repr__()@:
\begin{python}
>>> class Color(Enum):
...     RED = object()
...     GREEN = object()
...     BLUE = object()
...     def __repr__(self):
...         return "<%s.%s>" % (self.__class__.__name__, self._name_)
... 
>>> Color.GREEN
<Color.GREEN>
\end{python}

\subsection{Using a descriptive string}

Using a string as the value would look like:
\begin{python}
>>> class Color(Enum):
...     RED = 'stop'
...     GREEN = 'go'
...     BLUE = 'too fast!'
... 
>>> Color.GREEN
<Color.GREEN: 'go'>
\end{python}

\subsection{Using a custom \lstinline{__new__()}}

Using an auto-numbering @__new__()@ would look like:
\begin{python}
>>> class AutoNumber(Enum):
...     def __new__(cls):
...         value = len(cls.__members__) + 1
...         obj = object.__new__(cls)
...         obj._value_ = value
...         return obj
... 
>>> class Color(AutoNumber):
...     RED = ()
...     GREEN = ()
...     BLUE = ()
... 
>>> Color.GREEN
<Color.GREEN: 2>
\end{python}
To make a more general purpose @AutoNumber@, add @*args@ to the signature:
\begin{python}
>>> class AutoNumber(Enum):
...     def __new__(cls, *args):      # this is the only change from above
...         value = len(cls.__members__) + 1
...         obj = object.__new__(cls)
...         obj._value_ = value
...         return obj
\end{python}
Then when you inherit from @AutoNumber@ you can write your own @__init__@ to handle any extra arguments:
\begin{python}
>>> class Swatch(AutoNumber):
...     def __init__(self, pantone='unknown'):
...         self.pantone = pantone
...     AUBURN = '3497'
...     SEA_GREEN = '1246'
...     BLEACHED_CORAL = () # New color, no Pantone code yet!
... 
>>> Swatch.SEA_GREEN
<Swatch.SEA_GREEN: 2>
>>> Swatch.SEA_GREEN.pantone
'1246'
>>> Swatch.BLEACHED_CORAL.pantone
'unknown'
\end{python}
Note: The @__new__()@ method, if defined, is used during creation of the Enum members;
it is then replaced by Enum's @__new__()@ which is used after class creation for lookup of existing members.

Warning: Do not call @super().__new__()@, as the lookup-only @__new__@ is the one that is found;
instead, use the data type directly -- e.g.:
\begin{python}
obj = int.__new__(cls, value)
\end{python}

\subsection{OrderedEnum}

An ordered enumeration that is not based on @IntEnum@ and so maintains the normal @Enum@ invariants (such as not being comparable to other enumerations):
\begin{python}
>>> class OrderedEnum(Enum):
...     def __ge__(self, other):
...         if self.__class__ is other.__class__:
...             return self.value >= other.value
...         return NotImplemented
...     def __gt__(self, other):
...         if self.__class__ is other.__class__:
...             return self.value > other.value
...         return NotImplemented
...     def __le__(self, other):
...         if self.__class__ is other.__class__:
...             return self.value <= other.value
...         return NotImplemented
...     def __lt__(self, other):
...         if self.__class__ is other.__class__:
...             return self.value < other.value
...         return NotImplemented
... 
>>> class Grade(OrderedEnum):
...     A = 5
...     B = 4
...     C = 3
...     D = 2
...     F = 1
>>> Grade.C < Grade.A
True
\end{python}

\subsection{DuplicateFreeEnum}

Raises an error if a duplicate member value is found instead of creating an alias:
\begin{python}
>>> class DuplicateFreeEnum(Enum):
...     def __init__(self, *args):
...         cls = self.__class__
...         if any(self.value == e.value for e in cls):
...             a = self.name
...             e = cls(self.value).name
...             raise ValueError(
...                 "aliases not allowed in DuplicateFreeEnum:  %r --> %r"
...                 % (a, e))
>>> class Color(DuplicateFreeEnum):
...     RED = 1
...     GREEN = 2
...     BLUE = 3
...     GRENE = 2
... 
Traceback (most recent call last):
  ...
ValueError: aliases not allowed in DuplicateFreeEnum:  'GRENE' --> 'GREEN'
\end{python}
Note: This is a useful example for subclassing Enum to add or change other behaviors as well as disallowing aliases.
If the only desired change is disallowing aliases, the @unique()@ decorator can be used instead.

\subsection{Planet}

If @__new__()@ or @__init__()@ is defined, the value of the enum member will be passed to those methods:
\begin{figure}
\begin{python}
from enum import Enum
class Planet(Enum):
	MERCURY = ( 3.303E23, 2.4397E6 )
	VENUS       = ( 4.869E24, 6.0518E6 )
	EARTH       = (5.976E24, 6.37814E6)
	MARS         = (6.421E23, 3.3972E6)
	JUPITER    = (1.9E27,   7.1492E7)
	SATURN     = (5.688E26, 6.0268E7)
	URANUS    = (8.686E25, 2.5559E7)
	NEPTUNE  = (1.024E26, 2.4746E7)
	def __init__( self, mass, radius ):
		self.mass = mass		# in kilograms
		self.radius = radius	# in meters
	def surface_gravity( self ):
		# universal gravitational constant  (m3 kg-1 s-2)
		G = 6.67300E-11
		return G * self.mass / (self.radius * self.radius)
for p in Planet:
	print( f"{p.name}: {p.value}" )

MERCURY: (3.303e+23, 2439700.0)
VENUS: (4.869e+24, 6051800.0)
EARTH: (5.976e+24, 6378140.0)
MARS: (6.421e+23, 3397200.0)
JUPITER: (1.9e+27, 71492000.0)
SATURN: (5.688e+26, 60268000.0)
URANUS: (8.686e+25, 25559000.0)
NEPTUNE: (1.024e+26, 24746000.0)
\end{python}
\caption{Python Planet Example}
\label{f:PythonPlanetExample}
\end{figure}


\subsection{TimePeriod}

An example to show the @_ignore_@ attribute in use:
\begin{python}
>>> from datetime import timedelta
>>> class Period(timedelta, Enum):
...     "different lengths of time"
...     _ignore_ = 'Period i'
...     Period = vars()
...     for i in range(367):
...         Period['day_%d' % i] = i
... 
>>> list(Period)[:2]
[<Period.day_0: datetime.timedelta(0)>, <Period.day_1: datetime.timedelta(days=1)>]
>>> list(Period)[-2:]
[<Period.day_365: datetime.timedelta(days=365)>, <Period.day_366: datetime.timedelta(days=366)>]
\end{python}

\subsection{Subclassing EnumType}

While most enum needs can be met by customizing @Enum@ subclasses, either with class decorators or custom functions, @EnumType@ can be subclassed to provide a different Enum experience.


\section{OCaml}

% https://ocaml.org/docs/basic-data-types#enumerated-data-types
% https://dev.realworldocaml.org/runtime-memory-layout.html

OCaml provides a variant (union) type, where multiple heterogeneously-typed objects share the same storage.
The simplest form of the variant type is a list of nullary datatype constructors, which is like an unscoped, opaque enumeration.

OCaml provides a variant (union) type, which is an aggregation of heterogeneous types.
A basic variant is a list of nullary datatype constructors, which is like an unscoped, opaque enumeration.
\begin{ocaml}
type weekday = Mon | Tue | Wed | Thu | Fri | Sat | Sun
let day : weekday @= Mon@				$\C{(* bind *)}$
let take_class( d : weekday ) =
	@match@ d with						$\C{(* matching *)}$
		Mon | Wed -> Printf.printf "CS442\n" |
		Tue | Thu -> Printf.printf "CS343\n" |
		Fri -> Printf.printf "Tutorial\n" |
		_ -> Printf.printf "Take a break\n"
let _ = take_class( Mon );
@CS442@
\end{ocaml}
The only operations are binding and pattern matching (equality), where the variant name is logically the implementation tag stored in the union for discriminating the value in the object storage.
After compilation, variant names are mapped to an opague ascending intergral type discriminants, starting from 0.
Here, function @take_class@ has a @weekday@ parameter, and returns @"CS442"@, if the weekday value is @Mon@ or @Wed@, @"CS343"@, if the value is @Tue@ or @Thu@, and @"Tutorial"@ for @Fri@.
The ``@_@'' is a wildcard matching any @weekday@ value, so the function returns @"Take a break"@ for values @Sat@ or @Sun@, which are not matched by the previous cases.
Since the variant has no type, it has a \newterm{0-arity constructor}, \ie no parameters.
Because @weekday@ is a union of values @Mon@ to @Sun@, it is a \newterm{union type} in turns of the functional-programming paradigm. 

Each variant can have an associated heterogeneous type, with an n-ary constructor for creating a corresponding value.
\begin{ocaml}
type colour = Red | Green of @string@ | Blue of @int * float@
\end{ocaml}
A variant with parameter is stored in a memory block, prefixed by an int tag and has its parameters stores as words in the block.
@colour@ is a summation of a nullary type, a unary product type of @string@, and a cross product of @int@ and @float@.
(Mathematically, a @Blue@ value is a Cartesian product of the types @int@ type and @float@.)
Hence, a variant type creates a sum of product of different types.
\begin{ocaml}
let c = Red								$\C{(* 0-ary constructor, set tag *)}$
let _ = match c with Red -> Printf.printf "Red, "
let c = Green( "abc" )					$\C{(* 1-ary constructor, set tag *)}$
let _ = match c with Green g -> Printf.printf "%s, " g
let c = Blue( 1, 1.5 )					$\C{(* 2-ary constructor, set tag *)}$
let _ = match c with Blue( i, f ) -> Printf.printf "%d %g\n" i f
@Red, abc, 1 1.5@
\end{ocaml}

A variant type can have a recursive definition.
\begin{ocaml}
type @stringList@ = Empty | Pair of string * @stringList@
\end{ocaml}
which is a recursive sum of product of types, called an \newterm{algebraic data-type}.
A recursive function is often used to pattern match against a recursive variant type.
\begin{ocaml}
let rec @len_of_string_list@( list : stringList ): int =
	match list with
		Empty -> 0 |
		Pair( _ , r ) -> 1 + @len_of_string_list@ r
\end{ocaml}
Here, the head of the recursive type is removed and the remainder is processed until the type is empty.
Each recursion is counted to obtain the number of elements in the type.

Note, the compiler statically guarantees that only the correct kind of type is used in the \lstinline[language=OCaml]{match} statement.
However, the union tag is dynamically set on binding (and possible reset on assignment), so a \lstinline[language=OCaml]{match} statement is effectively doing RTTI to select the matching case clause.

In summary, an OCaml variant is a singleton value rather than a set of possibly ordered values, and hence, has no notion of enumerabilty.
Therefore it is not an enumeration, except for the simple opaque (nullary) case.

\begin{comment}
Date: Wed, 13 Mar 2024 10:52:34 -0400
Subject: Re: OCaml
To: "Peter A. Buhr" <pabuhr@uwaterloo.ca>
From: Gregor Richards <gregor.richards@uwaterloo.ca>

On 3/12/24 18:34, Peter A. Buhr wrote:
> Gregor, attached is a section Jiada wrote on OCaml (1-page).
> Does it reflect our discussion about functional languages and enumerations?

Yeah, I think so. The most important part, i.e., that once they're
parameterized they're not really enumerations at all, is covered clearly
enough.

A couple quibbles:

<<a list of untyped tags>>

This is true, but leaking implementation details. These are nullary datatype
constructors. Indeed, you later talk about "tagged variants", which are really
just parameterized variants, using the term "tag" differently, confusing the
term "tag" further.

<<Because weekday is a summation of values Mon to Sun, it is a sum type in
turns of the functional-programming paradigm>>

It is a *union* of values and is a *union* type.

With valediction,
  - Gregor Richards


Date: Thu, 14 Mar 2024 21:45:52 -0400
Subject: Re: OCaml "enums" do come with ordering
To: "Peter A. Buhr" <pabuhr@uwaterloo.ca>
From: Gregor Richards <gregor.richards@uwaterloo.ca>

On 3/14/24 21:30, Peter A. Buhr wrote:
> I've marked 3 places with your name to shows places with enum ordering.
>
> type weekday = Mon | Tue | Wed | Thu | Fri | Sat | Sun
> let day : weekday = Mon
> let take_class( d : weekday ) =
> 	if d <= Fri then				(* Gregor *)
> 		Printf.printf "weekday\n"
> 	else if d >= Sat then			(* Gregor *)
> 		Printf.printf "weekend\n";
> 	match d with
> 		Mon | Wed -> Printf.printf "CS442\n" |
> 		Tue | Thu -> Printf.printf "CS343\n" |
> 		Fri -> Printf.printf "Tutorial\n" |
> 		_ -> Printf.printf "Take a break\n"
>
> let _ = take_class( Mon ); take_class( Sat );
>
> type colour = Red | Green of string | Blue of int * float
> let c = Red
> let _ = match c with Red -> Printf.printf "Red, "
> let c = Green( "abc" )
> let _ = match c with Green g -> Printf.printf "%s, " g
> let c = Blue( 1, 1.5 )
> let _ = match c with Blue( i, f ) -> Printf.printf "%d %g\n" i f
>
> let check_colour(c: colour): string =
> 	if c < Green( "xyz" ) then		(* Gregor *)
> 		Printf.printf "green\n";
> 	match c with
> 		Red -> "Red" |
> 		Green g -> g |
> 		Blue(i, f) -> string_of_int i ^ string_of_float f
> let _ = check_colour( Red ); check_colour( Green( "xyz" ) );
>
> type stringList = Empty | Pair of string * stringList
> let rec len_of_string_list(l: stringList): int =
> 	match l with
> 		Empty -> 0 |
> 		Pair(_ , r) -> 1 + len_of_string_list r
>
> let _ = for i = 1 to 10 do
> 	Printf.printf "%d, " i
> done
>
> (* Local Variables: *)
> (* tab-width: 4 *)
> (* compile-command: "ocaml test.ml" *)
> (* End: *)

My functional-language familiarity is far more with Haskell than OCaml.  I
mostly view OCaml through a lens of "it's Haskell but with cheating".  Haskell
"enums" (ADTs) aren't ordered unless you specifically and manually put them in
the Ord typeclass by defining the comparators.  Apparently, OCaml has some
other rule, which I would guess is something like "sort by tag then by order of
parameter". Having a default behavior for comparators is *bizarre*; my guess
would be that it gained this behavior in its flirtation with object
orientation, but that's just a guess (and irrelevant).

This gives a total order, but not enumerability (which would still be
effectively impossible or even meaningless since enums are just a special case
of ADTs).

With valediction,
  - Gregor Richards

Date: Wed, 20 Mar 2024 18:16:44 -0400
Subject: Re:
To: "Peter A. Buhr" <pabuhr@uwaterloo.ca>
From: Gregor Richards <gregor.richards@uwaterloo.ca>


On 3/20/24 17:26, Peter A. Buhr wrote:
> Gregor, everyone at this end would like a definition of "enumerability". Can
> you formulate one?

According to the OED (emphasis added to the meaning I'm after):

enumerate (verb, transitive). To count, ascertain the number of; **more
usually, to mention (a number of things or persons) separately, as if for the
purpose of counting**; to specify as in a list or catalogue.

With C enums, if you know the lowest and highest value, you can simply loop
over them in a for loop (this is, of course, why so many enums come with an
ENUM_WHATEVER_LAST value). But, I would be hesitant to use the word "loop" to
describe enumerability, since in functional languages, you would recurse for
such a purpose.

In Haskell, in order to do something with every member of an "enumeration", you
would have to explicitly list them all. The type system will help a bit since
it knows if you haven't listed them all, but you would have to statically have
every element in the enumeration.  If somebody added new elements to the
enumeration later, your code to enumerate over them would no longer work
correctly, because you can't simply say "for each member of this enumeration do
X". In Haskell that's because there aren't actually enumerations; what they use
as enumerations are a degenerate form of algebraic datatypes, and ADTs are
certainly not enumerable. In OCaml, you've demonstrated that they impose
comparability, but I would still assume that you can't make a loop over every
member of an enumeration. (But, who knows!)

Since that's literally what "enumerate" means, it seems like a rather important
property for enumerations to have ;)

With valediction,
  - Gregor Richards


From: Andrew James Beach <ajbeach@uwaterloo.ca>
To: Gregor Richards <gregor.richards@uwaterloo.ca>, Peter Buhr <pabuhr@uwaterloo.ca>
CC: Michael Leslie Brooks <mlbrooks@uwaterloo.ca>, Fangren Yu <f37yu@uwaterloo.ca>,
    Jiada Liang <j82liang@uwaterloo.ca>
Subject: Re: Re:
Date: Thu, 21 Mar 2024 14:26:36 +0000

Does this mean that not all enum declarations in C create enumerations? If you
declare an enumeration like:

enum Example {
    Label,
    Name = 10,
    Tag = 3,
};

I don't think there is any way to enumerate (iterate, loop, recurse) over these
values without listing all of them.


Date: Thu, 21 Mar 2024 10:31:49 -0400
Subject: Re:
To: Andrew James Beach <ajbeach@uwaterloo.ca>, Peter Buhr <pabuhr@uwaterloo.ca>
CC: Michael Leslie Brooks <mlbrooks@uwaterloo.ca>, Fangren Yu <f37yu@uwaterloo.ca>,
    Jiada Liang <j82liang@uwaterloo.ca>
From: Gregor Richards <gregor.richards@uwaterloo.ca>

I consider this conclusion reasonable. C enums can be nothing more than const
ints, and if used in that way, I personally wouldn't consider them as
enumerations in any meaningful sense, particularly since the type checker
essentially does nothing for you there. Then they're a way of writing consts
repeatedly with some textual indicator that these definitions are related; more
namespace, less enum.

When somebody writes bitfield members as an enum, is that *really* an
enumeration, or just a use of the syntax for enums to keep related definitions
together?

With valediction,
  - Gregor Richards


Date: Tue, 16 Apr 2024 11:04:51 -0400
Subject: Re: C unnamed enumeration
To: "Peter A. Buhr" <pabuhr@uwaterloo.ca>
CC: <ajbeach@uwaterloo.ca>, <j82liang@uwaterloo.ca>, <mlbrooks@uwaterloo.ca>,
        <f37yu@uwaterloo.ca>
From: Gregor Richards <gregor.richards@uwaterloo.ca>

On 4/16/24 09:55, Peter A. Buhr wrote:
> So what is a variant? Is it a set of tag names, which might be a union or is it
> a union, which might have tag names?

Your tagless variant bears no resemblance to variants in any functional
programming language. A variant is a tag AND a union. You might not need to put
anything in the union, in which case it's a pointless union, but the named tag
is absolutely mandatory. That's the thing that varies.

I was unaware of std::variant. As far as functional languages are concerned,
std::variant IS NOT A VARIANT. Perhaps it would be best to use the term ADT for
the functional language concept, because that term has no other meanings.

An ADT cannot not have a named tag. That's meaningless. The tag is the data
constructor, which is the thing you actually define when you define an ADT. It
is strictly the union that's optional.

With valediction,
  - Gregor Richards
\end{comment}


\section{Comparison}

\VRef[Table]{t:FeatureLanguageComparison} shows a comparison of enumeration features and programming languages.
The features are high level and may not capture nuances within a particular language
The @const@ feature is simple macros substitution and not a typed enumeration.

\begin{table}
\caption{Enumeration Feature / Language Comparison}
\label{t:FeatureLanguageComparison}
\small
\setlength{\tabcolsep}{3pt}
\newcommand{\CM}{\checkmark}
\begin{tabular}{r|c|c|c|c|c|c|c|c|c|c|c|c|c}
				&Pascal	& Ada	&\Csharp& OCaml	& Java	&Modula-3&Golang& Rust	& Swift	& Python& C		& \CC	& \CFA	\\
\hline
@const@			& \CM	&		&		&		&		&		& \CM	&	  	&		&		&		& \CM	&		\\
\hline
\hline
opaque			&		&		&		&		&		&		&		&	  	&		&		&		&		& \CM	\\
\hline
typed			&		&		&		&		&		&		&		&	  	&		&		& @int@	& integral	& @T@	\\
\hline
safe			&		&		&		&		&		&		&		&	  	&		&		&		& \CM	& \CM	\\
\hline
ordered			&		&		&		&		&		&		&		&	  	&		&		& \CM	& \CM	& \CM	\\
\hline
dup. values		&		&		&		&		&		&		&		&	  	&		& alias	& \CM	& \CM	& \CM	\\
\hline
setable			&		&		&		&		&		&		&		&	  	&		&		& \CM	& \CM	& \CM	\\
\hline
auto-init		&		&		&		&		&		&		&		&	  	&		&		& \CM	& \CM	& \CM	\\
\hline
(un)scoped		&		&		&		&		&		&		&		&	  	&		&		& U		& U/S	& U/S	\\
\hline
overload		&		& \CM	&		&		&		&		&		&	  	&		&		&		& \CM	& \CM	\\
\hline
switch			&		&		&		&		&		&		&		&	  	&		&		& \CM	& \CM	& \CM	\\
\hline
loop			&		&		&		&		&		&		&		&	  	&		&		&		&		& \CM	\\
\hline
array			&		&		&		&		&		&		&		&	  	&		&		& \CM	&		& \CM	\\
\hline
subtype			&		&		&		&		&		&		&		&	  	&		&		&		&		& \CM	\\
\hline
inheritance		&		&		&		&		&		&		&		&	  	&		&		&		&		& \CM	\\
\end{tabular}
\end{table}
