source: doc/theses/jiada_liang_MMath/relatedwork.tex @ ec20ab9

Last change on this file since ec20ab9 was ec20ab9, checked in by Peter A. Buhr <pabuhr@…>, 4 months ago

small updates, and more proofreading of the related-works chapter

  • Property mode set to 100644
File size: 97.4 KB
Line 
1\chapter{Related Work}
2\label{s:RelatedWork}
3
4\begin{comment}
5An algebraic data type (ADT) can be viewed as a recursive sum of product types.
6A sum type lists values as members.
7A member in a sum type definition is known as a data constructor.
8For example, C supports sum types union and enumeration (enum).
9An enumeration in C can be viewed as the creation of a list of zero-arity data constructors.
10A union instance holds a value of one of its member types.
11Defining a union does not generate new constructors.
12The definition of member types and their constructors are from the outer lexical scope.
13
14In general, an \newterm{algebraic data type} (ADT) is a composite type, \ie, a type formed by combining other types.
15Three 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).
16Enumerated 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.
17Values of algebraic types are access by subscripting, field qualification, or type (pattern) matching.
18\end{comment}
19
20Enumeration-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}, Rust~\cite{Rust}, Swift~\cite{Swift}, Python~\cite{Python}.
21Among theses languages, there are a large set of overlapping features, but each language has its own unique extensions and restrictions.
22
23\section{Pascal}
24\label{s:Pascal}
25
26Classic Pascal introduced the \lstinline[language=Pascal]{const} aliasing declaration binding a name to a constant literal/expression.
27\begin{pascal}
28const one = 0 + 1;   Vowels = set of (A,E,I,O,U);   NULL = NIL;
29                 PI = 3.14159;   Plus = '+';   Fred = 'Fred';
30\end{pascal}
31As stated, this mechanism is not an enumeration because there is no specific type (pseudo enumeration).
32Hence, there is no notion of a (possibly ordered) set, modulo the \lstinline[language=pascal]{set of} type.
33The type of each constant name (enumerator) is inferred from the constant-expression type.
34
35Free Pascal~\cite[\S~3.1.1]{FreePascal} is a modern, object-oriented version of classic Pascal, with a C-style enumeration type.
36Enumerators must be assigned in ascending numerical order with a constant expression and the range can be non-consecutive.
37\begin{pascal}
38Type EnumType = ( one, two, three, forty @= 40@, fortyone );
39\end{pascal}
40Pseudo-functions @Pred@ and @Succ@ can only be used if the range is consecutive.
41The 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.
42The integral size can be explicitly specified using compiler directive @$PACKENUM@~$N$, where $N$ is the number of bytes, \eg:
43\begin{pascal}
44Type @{$\color{red}\$$PACKENUM 1}@ SmallEnum = ( one, two, three );
45            @{$\color{red}\$$PACKENUM 4}@ LargeEnum = ( BigOne, BigTwo, BigThree );
46Var S : SmallEnum; { 1 byte }
47          L : LargeEnum; { 4 bytes}
48\end{pascal}
49
50
51\section{Ada}
52
53An Ada enumeration type is a set of ordered unscoped identifiers (enumerators) bound to \emph{unique} \newterm{literals}.\footnote{%
54Ada 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).}
55\begin{ada}
56type Week is ( Mon, Tue, Wed, Thu, Fri, Sat, Sun ); -- literals (enumerators)
57\end{ada}
58Object initialization and assignment are restricted to the enumerators of this type.
59While Ada enumerators are unscoped, like C, Ada enumerators are overloadable.
60\begin{ada}
61type RGB is ( @Red@, @Green@, Blue );
62type Traffic_Light is ( @Red@, Yellow, @Green@ );
63\end{ada}
64Like \CFA, Ada uses a type-resolution algorithm including the left-hand side of assignmente to disambiguate among overloaded identifiers.
65\VRef[Figure]{f:AdaEnumeration} shows how ambiguity is handled using a cast, \ie \lstinline[language=ada]{RGB'(Red)}.
66
67\begin{figure}
68\begin{ada}
69with Ada.Text_IO; use Ada.Text_IO;
70procedure test is
71        type RGB is ( @Red@, Green, Blue );
72        type Traffic_Light is ( @Red@, Yellow, Green );         -- overload
73        procedure @Red@( Colour : RGB ) is begin            -- overload
74                Put_Line( "Colour is " & RGB'Image( Colour ) );
75        end Red;
76        procedure @Red@( TL : Traffic_Light ) is begin       -- overload
77                Put_Line( "Light is " & Traffic_Light'Image( TL ) );
78        end Red;
79begin
80        @Red@( Blue );                           -- RGB
81        @Red@( Yellow );                                -- Traffic_Light
82        @Red@( @RGB'(Red)@ );           -- ambiguous without cast
83end test;
84\end{ada}
85\caption{Ada Enumeration Overload Resolution}
86\label{f:AdaEnumeration}
87\end{figure}
88
89Enumerators without initialization are auto-initialized from left to right, starting at zero, incrementing by 1.
90Enumerators with initialization must set \emph{all} enumerators in \emph{ascending} order, \ie there is no auto-initialization.
91\begin{ada}
92type Week is ( Mon, Tue, Wed, Thu, Fri, Sat, Sun );
93for Week use ( Mon => 0, Tue => 1, Wed => 2, Thu => @10@, Fri => 11, Sat => 14, Sun => 15 );
94\end{ada}
95The enumeration operators are the equality and relational operators, @=@, @/=@, @<@, @<=@, @=@, @/=@, @>=@, @>@, where the ordering relationship is given implicitly by the sequence of acsending enumerators.
96
97Ada provides an alias mechanism, \lstinline[language=ada]{renames}, for aliasing types, which is useful to shorten package identifiers.
98\begin{ada}
99@OtherRed@ : RGB renames Red;
100\end{ada}
101which suggests a possible \CFA extension to @typedef@.
102\begin{cfa}
103typedef RGB.Red OtherRed;
104\end{cfa}
105
106There are three pairs of inverse enumeration pseudo-functions (attributes): @'Pos@ and @'Val@, @'Enum_Rep@ and @'Enum_Val@, and @'Image@ and @'Value@,
107\begin{cquote}
108\setlength{\tabcolsep}{15pt}
109\begin{tabular}{@{}ll@{}}
110\begin{ada}
111RGB'Pos( Red ) = 0;
112RGB'Enum_Rep( Red ) = 10;
113RGB'Image( Red ) = "RED";
114\end{ada}
115&
116\begin{ada}
117RGB'Val( 0 ) = Red
118RGB'Enum_Val( 10 ) =  Red
119RGB'Value( "Red" ) =  Red
120\end{ada}
121\end{tabular}
122\end{cquote}
123These attributes are important for IO.
124An 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.
125
126Ada allows the enumerator label to be a character constant.
127\begin{ada}
128type Operator is ( '+', '-', '*', '/' );
129\end{ada}
130which is syntactic sugar for the label and not character literals from the predefined type @Character@.
131The purpose is strictly readability using character literals rather than identifiers.
132\begin{ada}
133Op : Operator := '+';
134if Op = '+' or else Op = '-' then ... ;
135elsif Op = '*' or else Op = '/' then ... ; end if;
136\end{ada}
137Interestingly, arrays of character enumerators can be treated as strings.
138\begin{ada}
139Ops : array( 0..3 ) of Operator;
140Ops := @"+-*/"@;            -- string assignment to array elements
141Ops := "+-" @&@ "*/";   -- string concatenation and assignment
142\end{ada}
143Ada's @Character@ type is defined as a character enumeration across all Latin-1 characters.
144
145Ada's boolean type is also a special enumeration, which can be used in conditions.
146\begin{ada}
147type Boolean is (False, True); -- False / True not keywords
148@Flag@ : Boolean;
149if @Flag@ then ...    -- conditional
150\end{ada}
151Since only types derived from @Boolean@ can be a conditional, @Boolean@ is essentially  a builtin type.
152
153Ada provides \emph{consecutive} subtyping of an enumeration using \lstinline[language=ada]{range}.
154\begin{ada}
155type Week is ( Mon, Tue, Wed, Thu, Fri, Sat, Sun );
156subtype Weekday is Week @range Mon .. Fri@;
157subtype Weekend is Week @range Sat .. Sun@;
158Day : Week;
159\end{ada}
160Hence, the ordering of the enumerators is crucial to provide the necessary ranges.
161
162An enumeration type can be used in the Ada \lstinline[language=ada]{case} (all enumerators must appear or a @default@) or iterating constructs.
163\begin{cquote}
164\setlength{\tabcolsep}{15pt}
165\begin{tabular}{@{}ll@{}}
166\begin{ada}
167case Day is
168        when @Mon .. Fri@ => ... ;
169        when @Sat .. Sun@ => ... ;
170end case;
171\end{ada}
172&
173\begin{ada}
174case Day is
175        when @Weekday@ => ... ;  -- subtype ranges
176        when @Weekend@ => ... ;
177end case;
178\end{ada}
179\end{tabular}
180\end{cquote}
181
182\begin{cquote}
183\setlength{\tabcolsep}{12pt}
184\begin{tabular}{@{}lll@{}}
185\begin{ada}
186for Day in @Mon .. Sun@ loop
187        ...
188end loop;
189\end{ada}
190&
191\begin{ada}
192for Day in @Weekday@ loop
193        ...
194end loop;
195\end{ada}
196&
197\begin{ada}
198for Day in @Weekend@ loop
199        ...
200end loop;
201\end{ada}
202\end{tabular}
203\end{cquote}
204
205An enumeration type can be used as an array dimension and subscript.
206\begin{ada}
207Lunch : array( @Week@ ) of Time;
208for Day in Week loop
209        Lunch( @Day@ ) := ... ;       -- set lunch time
210end loop;
211\end{ada}
212
213
214\section{\CC}
215\label{s:C++RelatedWork}
216
217\CC enumeration is largely backwards compatible with C, so it inherited C's enumerations with some modifications and additions.
218
219\CC has aliasing using @const@ declarations, like C \see{\VRef{s:Cconst}}, with type inferencing, plus static/dynamic initialization.
220(Note, a \CC @constexpr@ declaration is the same as @const@ with the restriction that the initialization is a compile-time expression.)
221\begin{c++}
222const @auto@ one = 0 + 1;                               $\C{// static initialization}$
223const @auto@ NIL = nullptr;
224const @auto@ PI = 3.14159;
225const @auto@ Plus = '+';
226const @auto@ Fred = "Fred";
227const @auto@ Mon = 0, Tue = Mon + 1, Wed = Tue + 1, Thu = Wed + 1, Fri = Thu + 1,
228                                Sat = Fri + 1, Sun = Sat + 1;
229void foo() {
230        const @auto@ r = random();                      $\C{// dynamic initialization}$
231        int va[r];                                                      $\C{// VLA, auto scope only}$
232}
233\end{c++}
234Statically initialized identifiers may appear in any constant-expression context, \eg @case@.
235Dynamically initialized identifiers may appear as array dimensions in @g++@, which allows variable-sized arrays.
236Interestingly, global \CC @const@ declarations are implicitly marked @static@ (@r@, read-only local, rather than @R@, read-only external)
237\begin{c++}
238$\$$ nm test.o
2390000000000000018 @r@ Mon
240\end{c++}
241whereas C @const@ declarations without @static@ are marked @R@.
242
243The following \CC non-backwards compatible changes are made \see{\cite[\S~7.2]{ANSI98:c++}}.
244\begin{cquote}
245Change: \CC objects of enumeration type can only be assigned values of the same enumeration type.
246In C, objects of enumeration type can be assigned values of any integral type. \\
247Example:
248\begin{c++}
249enum color { red, blue, green };
250color c = 1;                                                    $\C{// valid C, invalid c++}$
251\end{c++}
252\textbf{Rationale}: The type-safe nature of \CC. \\
253\textbf{Effect on original feature}: Deletion of semantically well-defined feature. \\
254\textbf{Difficulty of converting}: Syntactic transformation. (The type error produced by the assignment can be automatically corrected by applying an explicit cast.) \\
255\textbf{How widely used}: Common.
256\end{cquote}
257
258\begin{cquote}
259Change: In \CC, the type of an enumerator is its enumeration.
260In C, the type of an enumerator is @int@. \\
261Example:
262\begin{c++}
263enum e { A };
264sizeof(A) == sizeof(int)                                $\C{// in C}$
265sizeof(A) == sizeof(e)                                  $\C{// in c++}$
266/* and sizeof(int) is not necessary equal to sizeof(e) */
267\end{c++}
268\textbf{Rationale}: In \CC, an enumeration is a distinct type. \\
269\textbf{Effect on original feature}: Change to semantics of well-defined feature. \\
270\textbf{Difficulty of converting}: Semantic transformation. \\
271\textbf{How widely used}: Seldom. The only time this affects existing C code is when the size of an enumerator is taken.
272Taking the size of an enumerator is not a common C coding practice.
273\end{cquote}
274Hence, the values in a \CC enumeration can only be its enumerators (without a cast).
275While the storage size of an enumerator is up to the compiler, there is still an implicit cast to @int@.
276\begin{c++}
277enum E { A, B, C };
278E e = A;
279int i = A;    i = e;                                    $\C{// implicit casts to int}$
280\end{c++}
281\CC{11} added a scoped enumeration, \lstinline[language=c++]{enum class} (or \lstinline[language=c++]{enum struct})\footnote{
282The use of keyword \lstinline[language=c++]{class} is resonable because default visibility is \lstinline[language=c++]{private} (scoped).
283However, default visibility for \lstinline[language=c++]{struct} is \lstinline[language=c++]{public} (unscoped) making it an odd choice.},
284where the enumerators are accessed using type qualification.
285\begin{c++}
286enum class E { A, B, C };
287E e = @E::@A;                                                   $\C{// qualified enumerator}$
288e = B;                                                                  $\C{// error: B not in scope}$
289\end{c++}
290\CC{20} supports explicit unscoping with a \lstinline[language=c++]{using enum} declaration.
291\begin{c++}
292enum class E { A, B, C };
293@using enum E;@
294E e = A;    e = B;                                              $\C{// direct access}$
295\end{c++}
296\CC{11} added the ability to explicitly declare only an underlying \emph{integral} type for \lstinline[language=c++]{enum class}.
297\begin{c++}
298enum class RGB @: long@ { Red, Green, Blue };
299enum class rgb @: char@ { Red = 'r', Green = 'g', Blue = 'b' };
300enum class srgb @: signed char@ { Red = -1, Green = 0, Blue = 1 };
301\end{c++}
302There is no implicit conversion from the \lstinline[language=c++]{enum class} type to its declared type.
303\begin{c++}
304rgb crgb = rgb::Red;
305char ch = rgb::Red;   ch = crgb;                $\C{// error}$
306\end{c++}
307An enumeration can be used in the @if@ and @switch@ statements.
308\begin{cquote}
309\setlength{\tabcolsep}{15pt}
310\begin{tabular}{@{}ll@{}}
311\begin{c++}
312if ( @day@ <= Fri )
313        cout << "weekday" << endl;
314
315
316
317
318\end{c++}
319&
320\begin{c++}
321switch ( @day@ ) {
322  case Mon: case Tue: case Wed: case Thu: case Fri:
323        cout << "weekday" << endl; break;
324  case Sat: case Sun:
325        cout << "weekend" << endl; break;
326}
327\end{c++}
328\end{tabular}
329\end{cquote}
330However, there is no mechanism to iterate through an enumeration without an unsafe cast and it does not understand the enumerator values.
331\begin{c++}
332enum Week { Mon, Tue, Wed, Thu = 10, Fri, Sat, Sun };
333for ( Week d = Mon; d <= Sun; d = @(Week)(d + 1)@ ) cout << d << ' ';
3340 1 2 @3 4 5 6 7 8 9@ 10 11 12 13
335\end{c++}
336An enumeration type cannot declare an array dimension but an enumerator can be used as a subscript.
337There is no mechanism to subtype or inherit from an enumeration.
338
339
340\section{C\raisebox{-0.7ex}{\LARGE$^\sharp$}\xspace} % latex bug: cannot use \relsize{2} so use \LARGE
341\label{s:Csharp}
342
343% https://www.tutorialsteacher.com/codeeditor?cid=cs-mk8Ojx
344% https://learn.microsoft.com/en-us/dotnet/api/system.enum?view=net-8.0
345% https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/enums
346
347\Csharp is a dynamically-typed programming-language with a scoped, integral enumeration similar to \CC \lstinline[language=C++]{enum class}.
348\begin{csharp}
349enum Week : @long@ { Mon, Tue, Wed, Thu@ = 10@, Fri, Sat, Sun@,@ } // terminating comma
350enum RGB { Red, Green, Blue }
351\end{csharp}
352The default underlying integral type is @int@ (no @char@), with auto-incrementing, implicit/explicit initialization, and terminating comma.
353A method cannot be defined in an enumeration type (extension methods are possible).
354There is an explicit bidirectional conversion between an enumeration and its integral type, and an implicit conversion to the enumerator label in display contexts.
355\begin{csharp}
356int iday = (int)Week.Fri;                       $\C{// day == 11}$
357Week day = @(Week)@42;                          $\C{// day == 42, unsafe}$
358string mon = Week.Mon.ToString();       $\C{// mon == "Mon"}$
359RGB rgb = RGB.Red;                                      $\C{// rgb == "Red"}$
360day = @(Week)@rgb;                                      $\C{// day == "Mon", unsafe}$
361Console.WriteLine( Week.Fri );          $\C{// print label Fri}$
362\end{csharp}
363The majority of the integral operators (relational and arithmetic) work with enumerations, except @*@ and @/@.
364\begin{csharp}
365day = day++ - 5;                                        $\C{// unsafe}$
366day = day & day;
367\end{csharp}
368
369An enumeration can be used in the @if@ and @switch@ statements.
370\begin{cquote}
371\setlength{\tabcolsep}{15pt}
372\begin{tabular}{@{}ll@{}}
373\begin{csharp}
374if ( @day@ <= Week.Fri )
375        Console.WriteLine( "weekday" );
376
377
378
379
380
381\end{csharp}
382&
383\begin{csharp}
384switch ( @day@ ) {
385  case Week.Mon: case Week.Tue: case Week.Wed:
386  case Week.Thu: case Week.Fri:
387        Console.WriteLine( "weekday" ); break;
388  case Week.Sat: case Week.Sun:
389        Console.WriteLine( "weekend" ); break;
390}
391\end{csharp}
392\end{tabular}
393\end{cquote}
394However, there is no mechanism to iterate through an enumeration without an unsafe cast to increment and positions versus values is not handled.
395\begin{csharp}
396for ( Week d = Mon; d <= Sun; @d += 1@ ) {
397        Console.Write( d + " " );
398}
399Mon Tue Wed @3 4 5 6 7 8 9@ Thu Fri Sat Sun
400\end{csharp}
401The @Enum.GetValues@ pseudo-method retrieves an array of the enumeration constants for looping over an enumeration type or variable (expensive operation).
402\begin{csharp}
403foreach ( Week d in @Enum.GetValues@( typeof(Week) ) ) {
404        Console.WriteLine( d + " " + (int)d + " " ); // label, position
405}
406Mon 0, Tue 1, Wed 2, Thu 10, Fri 11, Sat 12, Sun 13,
407\end{csharp}
408
409An enumeration type cannot declare an array dimension but an enumerator can be used as a subscript.
410There is no mechanism to subtype or inherit from an enumeration.
411
412The @Flags@ attribute creates a bit-flags enumeration, making bitwise operators @&@, @|@, @~@ (complement), @^@ (xor) sensible.
413\begin{csharp}
414@[Flags]@ public enum Week {
415        None = 0x0, Mon = 0x1, Tue = 0x2, Wed = 0x4,
416        Thu = 0x8, Fri = 0x10, Sat = 0x20, Sun = 0x40,
417        Weekdays = @Mon | Tue | Wed | Thu | Fri@ $\C{// Weekdays == 0x1f}$
418        Weekend = @Sat | Sun@,                  $\C{// Weekend == 0x60}$
419}
420Week meetings = @Week.Mon | Week.Wed@; $\C{// 0x5}$
421\end{csharp}
422
423
424\section{Golang}
425
426Golang has a no enumeration.
427It has @const@ aliasing declarations, similar to \CC \see{\VRef{s:C++RelatedWork}}, for basic types with type inferencing and static initialization (constant expression).
428\begin{Go}
429const R @int@ = 0;  const G @uint@ = 1;  const B = 2; $\C{// explicit typing and type inferencing}$
430const Fred = "Fred";  const Mary = "Mary";  const Jane = "Jane";
431const S = 0;  const T = 0;
432const USA = "USA";  const U = "USA";
433const V = 3.1;  const W = 3.1;
434\end{Go}
435Since these declarations are unmutable variables, they are unscoped and Golang has no overloading.
436
437Golang provides an enumeration-like feature to group together @const@ declaration into a block and introduces a form of auto-initialization.
438\begin{Go}
439const ( R = 0; G; B )                                   $\C{// implicit initialization: 0 0 0}$
440const ( Fred = "Fred"; Mary = "Mary"; Jane = "Jane" ) $\C{// explicit initialization: Fred Mary Jane}$
441const ( S = 0; T; USA = "USA"; U; V = 3.1; W ) $\C{// type change, implicit/explicit: 0 0 USA USA 3.1 3.1}$
442\end{Go}
443The first identifier \emph{must} be explicitly initialized;
444subsequent identifiers can be implicitly or explicitly initialized.
445Implicit initialization is the \emph{previous} (predecessor) identifier value.
446
447Each @const@ declaration provides an implicit integer counter starting at zero, called \lstinline[language=Go]{iota}.
448Using \lstinline[language=Go]{iota} outside of a @const@ block always sets the identifier to zero.
449\begin{Go}
450const R = iota;                                                 $\C{// 0}$
451\end{Go}
452Inside a @const@ block, \lstinline[language=Go]{iota} is implicitly incremented for each \lstinline[language=golang]{const} identifier and used to initialize the next uninitialized identifier.
453\begin{Go}
454const ( R = @iota@; G; B )                              $\C{// implicit: 0 1 2}$
455const ( C = @iota + B + 1@; G; Y )              $\C{// implicit: 3 4 5}$
456\end{Go}
457An underscore \lstinline[language=golang]{const} identifier advances \lstinline[language=Go]{iota}.
458\begin{Go}
459const ( O1 = iota + 1; @_@; O3; @_@; O5 ) // 1, 3, 5 
460\end{Go}
461Auto-initialization reverts from \lstinline[language=Go]{iota} to the previous value after an explicit initialization, but auto-incrementing of \lstinline[language=Go]{iota} continues.
462\begin{Go}
463const ( Mon = iota; Tue; Wed; // 0, 1, 2
464                @Thu = 10@; Fri; Sat; Sun = itoa ) // 10, 10, 10, 6
465\end{Go}
466Auto-initialization from \lstinline[language=Go]{iota} is restarted and \lstinline[language=Go]{iota} reinitialized with an expression containing as most \emph{one} \lstinline[language=Go]{iota}.
467\begin{Go}
468const ( V1 = iota; V2; @V3 = 7;@ V4 = @iota@ + 1; V5 ) // 0 1 7 4 5
469const ( Mon = iota; Tue; Wed; // 0, 1, 2
470                @Thu = 10;@ Fri = @iota - Wed + Thu - 1@; Sat; Sun ) // 10, 11, 12, 13
471\end{Go}
472Here, @V4@ and @Fri@ restart auto-incrementing from \lstinline[language=Go]{iota} and reset \lstinline[language=Go]{iota} to 4 and 11, respectively, because of the intialization expressions containing \lstinline[language=Go]{iota}.
473Note, because \lstinline[language=Go]{iota} is incremented for an explicitly initialized identifier or @_@,
474at @Fri@ \lstinline[language=Go]{iota} is 4 requiring the minus one to compute the value for @Fri@.
475
476Basic switch and looping are possible.
477\begin{cquote}
478\setlength{\tabcolsep}{20pt}
479\begin{tabular}{@{}ll@{}}
480\begin{Go}
481day := Mon;     // := $\(\Rightarrow\)$ type inferencing
482switch @day@ {
483  case Mon, Tue, Wed, Thu, Fri:
484        fmt.Println( "weekday" );
485  case Sat, Sun:
486        fmt.Println( "weekend" );
487}
488\end{Go}
489&
490\begin{Go}
491
492for i := @Mon@; i <= @Sun@; i += 1 {
493        fmt.Println( i )
494}
495
496
497
498\end{Go}
499\end{tabular}
500\end{cquote}
501However, the loop prints the values from 0 to 13 because there is no actual enumeration.
502
503A constant variable can be used as an array dimension or a subscript.
504\begin{Go}
505var ar[@Sun@] int
506ar[@Mon@] = 3
507\end{Go}
508
509
510\section{Java}
511
512Java provides an enumeration using a specialized class.
513A basic Java enumeration is an opaque enumeration, where the enumerators are constants.
514\begin{Java}
515enum Week {
516        Mon, Tue, Wed, Thu, Fri, Sat, Sun;
517}
518Week day = Week.Sat;
519\end{Java}
520The enumerators members are scoped and cannot be made \lstinline[language=java]{public}, hence require qualification.
521The value of an enumeration instance is restricted to its enumerators.
522
523The position (ordinal) and label are accessible but there is no value.
524\begin{Java}
525System.out.println( day.!ordinal()! + " " + !day! + " " + day.!name()! );
5265 Sat Sat
527\end{Java}
528Since @day@ has no value, it prints its label (name).
529The member @valueOf@ is the inverse of @name@ converting a string to enumerator.
530\begin{Java}
531day = Week.valueOf( "Wed" );
532\end{Java}
533Extra members can be added to provide specialized operations.
534\begin{Java}
535public boolean isWeekday() { return !ordinal()! <= Fri.ordinal(); }
536public boolean isWeekend() { return Fri.ordinal() < !ordinal()!; }
537\end{Java}
538Notice the unqualified calls to @ordinal@ in the members implying a \lstinline[language=Java]{this} to some implicit implementation variable, likely an @int@.
539
540Enumerator values require an enumeration type (any Java type may be used) and implementation member.
541\begin{Java}
542enum Week {
543        Mon!(1)!, Tue!(2)!, Wed!(3)!, Thu!(4)!, Fri!(5)!, Sat!(6)!, Sun!(7)!; // must appear first
544        private !long! day;                                     $\C{// enumeration type and implementation member}$
545        private Week( !long! d ) { day = d; } $\C{// enumerator initialization}$
546};
547Week day = Week.Sat;
548\end{Java}
549The position, value, and label are accessible.
550\begin{Java}
551System.out.println( !day.ordinal()! + " " + !day.day! + " " + !day.name()! );
5525 6 Sat
553\end{Java}
554If the implementation member is \lstinline[language=Java]{public}, the enumeration is unsafe, as any value of the underlying type can be assigned to it, \eg @day = 42@.
555The implementation constructor must be private since it is only used internally to initialize the enumerators.
556Initialization occurs at the enumeration-type declaration for each enumerator in the first line.
557
558Enumerations can be used in the @if@ and @switch@ statements but only for equality tests.
559\begin{cquote}
560\setlength{\tabcolsep}{15pt}
561\begin{tabular}{@{}ll@{}}
562\begin{Java}
563if ( !day! == Week.Fri )
564        System.out.println( "Fri" );
565
566
567
568
569\end{Java}
570&
571\begin{Java}
572switch ( !day! ) {
573  case Mon: case Tue: case Wed: case Thu: case Fri:
574        System.out.println( "weekday" );  break;
575  case Sat: case Sun:
576        System.out.println( "weekend" );  break;
577}
578\end{Java}
579\end{tabular}
580\end{cquote}
581Notice enumerators in the @switch@ statement do not require qualification.
582
583There are no arithemtic operations on enumerations, so there is no arithmetic way to iterate through an enumeration without making the implementation type \lstinline[language=Java]{public}.
584Like \Csharp, looping over an enumeration is done using method @values@, which returns an array of enumerator values (expensive operation).
585\begin{Java}
586for ( Week d : Week.values() ) {
587        System.out.print( d.ordinal() + d.day + " " +  d.name() + ",  " );
588}
5890 1 Mon,  1 2 Tue,  2 3 Wed,  3 4 Thu,  4 5 Fri,  5 6 Sat,  6 7 Sun, 
590\end{Java}
591
592An enumeration type cannot declare an array dimension nor can an enumerator be used as a subscript.
593Enumeration inheritence is disallowed because an enumeration is \lstinline[language=Java]{final}.
594
595Java 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.
596There is also a specialized version of @HashMap@ with enumerator keys, which has performance benefits.
597
598
599\section{Rust}
600
601% https://doc.rust-lang.org/reference/items/enumerations.html
602
603Rust @enum@ provides two largely independent mechanisms: an ADT and an enumeration.
604When @enum@ is an ADT, pattern matching is used to discriminate among the variant types.
605\begin{cquote}
606\sf\setlength{\tabcolsep}{20pt}
607\begin{tabular}{@{}ll@{}}
608\begin{rust}
609struct S {
610        i : isize,  j : isize
611}
612enum @ADT@ {
613        I(isize),   // int
614        F(f64),   // float
615        S(S),     // struct
616}
617\end{rust}
618&
619\begin{rust}
620let mut s = S{ i : 3, j : 4 };
621let mut adt : ADT;
622adt = ADT::I(3);  adt = ADT::F(3.5);  adt = ADT::S(s); // init examples
623@match@ adt {
624        ADT::I(i) => println!( "{:?}", i ),
625        ADT::F(f) => println!( "{:?}", f ),
626        ADT::S(s) => println!( "{:?} {:?}", s.i, s.j ),
627}
628\end{rust}
629\end{tabular}
630\end{cquote}
631When the variant types are the unit type, the ADT is still not an enumeration because there is no enumerating \see{\VRef{s:AlgebraicDataType}}.
632\begin{rust}
633enum Week { Mon, Tues, Wed, Thu, Fri, Sat, Sun@,@ } // terminating comma
634let mut week : Week = Week::Mon;
635match week {
636        Week::Mon => println!( "Mon" ),
637        ...
638        Week::Sun => println!( "Sun" ),
639}
640\end{rust}
641
642However, Rust allows direct setting of the ADT constructor, which means it is actually a tag.
643\begin{cquote}
644\sf\setlength{\tabcolsep}{15pt}
645\begin{tabular}{@{}ll@{}}
646\begin{rust}
647enum Week {
648        Mon, Tues, Wed, // start 0
649        Thu @= 10@, Fri,
650        Sat, Sun,
651}
652
653\end{rust}
654&
655\begin{rust}
656#[repr(u8)]
657enum ADT {
658        I(isize) @= 5@,  // ???
659        F(f64) @= 10@,
660        S(S) @= 0@,
661}
662\end{rust}
663\end{tabular}
664\end{cquote}
665Through this integral tag, it is possible to enumerate, and when all tags represent the unit type, it behaves like \CC \lstinline[language=C++]{enum class}.
666When tags represent non-unit types, Rust largely precludes accessing the tag because the semantics become meaningless.
667Hence, the two mechanisms are largely disjoint, and ony the enumeration component is discussed.
668
669In detail, the @enum@ type has an implicit integer tag (discriminant), with a unique value for each variant type.
670Direct initialization is by a compile-time expression generating a constant value.
671Indirect initialization (without initialization, @Fri@/@Sun@) is auto-initialized: from left to right, starting at zero or the next explicitly initialized constant, incrementing by @1@.
672There is an explicit cast from the tag to integer.
673\begin{rust}
674let mut mon : isize = Week::Mon as isize;
675\end{rust}
676An enumeration can be used in the @if@ and \lstinline[language=rust]{match} (@switch@) statements.
677\begin{cquote}
678\setlength{\tabcolsep}{8pt}
679\begin{tabular}{@{}ll@{}}
680\begin{c++}
681if @week as isize@ == Week::Mon as isize {
682        println!( "{:?}", week );
683}
684
685
686\end{c++}
687&
688\begin{c++}
689match @week@ {
690        Week::Mon | Week:: Tue | Week::Wed | Week::Thu
691                | Week::Fri => println!( "weekday" ),
692        Week::Sat | Week:: Sun => println!( "weekend" ),
693}
694\end{c++}
695\end{tabular}
696\end{cquote}
697However, there is no mechanism to iterate through an enumeration without an casting to integral and positions versus values is not handled.
698\begin{c++}
699for d in Week::Mon as isize ..= Week::Sun as isize {
700        print!( "{:?} ", d );
701}
7020 1 2 @3 4 5 6 7 8 9@ 10 11 12 13
703\end{c++}
704An enumeration type cannot declare an array dimension nor as a subscript.
705There is no mechanism to subtype or inherit from an enumeration.
706
707
708\section{Swift}
709
710% https://www.programiz.com/swift/online-compiler
711
712A 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.
713\begin{swift}
714enum Many {
715        case Mon, Tue, Wed, Thu, Fri, Sat, Sun // basic enumerator
716        case code( String ) // string enumerator
717        case tuple( Int, Int, Int ) // tuple enumerator
718};
719var day = Many.Sat; // qualification to resolve type
720print( day );
721day = .Wed // no qualification after type resolved
722print( day );
723day = .code( "ABC" );
724print( day );
725day = .tuple( 1, 2, 3 );
726print( day );
727
728Sat
729Wed
730code("ABC")
731tuple(1, 2, 3)
732\end{swift}
733
734
735An 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.
736
737If you are familiar with C, you will know that C enumerations assign related names to a set of integer values.
738Enumerations in Swift are much more flexible, and don't have to provide a value for each case of the enumeration.
739If 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.
740
741Alternatively, 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.
742You 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.
743
744Enumerations in Swift are first-class types in their own right.
745They 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.
746Enumerations can also define initializers to provide an initial case value;
747can be extended to expand their functionality beyond their original implementation; and can conform to protocols to provide standard functionality.
748
749For more about these capabilities, see Properties, Methods, Initialization, Extensions, and Protocols.
750
751\paragraph{Enumeration Syntax}
752
753
754Note:
755Swift enumeration cases don't have an integer value set by default, unlike languages like C and Objective-C.
756In the CompassPoint example above, @north@, @south@, @east@ and @west@ don't implicitly equal 0, 1, 2 and 3.
757Instead, the different enumeration cases are values in their own right, with an explicitly defined type of CompassPoint.
758
759Multiple cases can appear on a single line, separated by commas:
760\begin{swift}
761enum Planet {
762        case mercury, venus, earth, mars, jupiter, saturn, uranus, neptune
763}
764\end{swift}
765Each enumeration definition defines a new type.
766Like other types in Swift, their names (such as @CompassPoint@ and @Planet@) start with a capital letter.
767Give enumeration types singular rather than plural names, so that they read as self-evident:
768\begin{swift}
769var directionToHead = CompassPoint.west
770\end{swift}
771The type of @directionToHead@ is inferred when it's initialized with one of the possible values of @CompassPoint@.
772Once @directionToHead@ is declared as a @CompassPoint@, you can set it to a different @CompassPoint@ value using a shorter dot syntax:
773\begin{swift}
774directionToHead = .east
775\end{swift}
776The type of @directionToHead@ is already known, and so you can drop the type when setting its value.
777This makes for highly readable code when working with explicitly typed enumeration values.
778
779\paragraph{Matching Enumeration Values with a Switch Statement}
780
781You can match individual enumeration values with a switch statement:
782\begin{swift}
783directionToHead = .south
784switch directionToHead {
785case .north:
786        print("Lots of planets have a north")
787case .south:
788        print("Watch out for penguins")
789case .east:
790        print("Where the sun rises")
791case .west:
792        print("Where the skies are blue")
793}
794// Prints "Watch out for penguins"
795\end{swift}
796You can read this code as:
797\begin{quote}
798"Consider the value of directionToHead.
799In the case where it equals @.north@, print "Lots of planets have a north".
800In the case where it equals @.south@, print "Watch out for penguins"."
801
802...and so on.
803\end{quote}
804As described in Control Flow, a switch statement must be exhaustive when considering an enumeration's cases.
805If the case for @.west@ is omitted, this code doesn't compile, because it doesn't consider the complete list of @CompassPoint@ cases.
806Requiring exhaustiveness ensures that enumeration cases aren't accidentally omitted.
807
808When 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:
809\begin{swift}
810let somePlanet = Planet.earth
811switch somePlanet {
812case .earth:
813        print("Mostly harmless")
814default:
815        print("Not a safe place for humans")
816}
817// Prints "Mostly harmless"
818\end{swift}
819
820\paragraph{Iterating over Enumeration Cases}
821
822For some enumerations, it's useful to have a collection of all of that enumeration's cases.
823You enable this by writing @CaseIterable@ after the enumeration's name.
824Swift exposes a collection of all the cases as an allCases property of the enumeration type.
825Here's an example:
826\begin{swift}
827enum Beverage: CaseIterable {
828        case coffee, tea, juice
829}
830let numberOfChoices = Beverage.allCases.count
831print("\(numberOfChoices) beverages available")
832// Prints "3 beverages available"
833\end{swift}
834In the example above, you write @Beverage.allCases@ to access a collection that contains all of the cases of the @Beverage@ enumeration.
835You 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.
836The example above counts how many cases there are, and the example below uses a for-in loop to iterate over all the cases.
837\begin{swift}
838for beverage in Beverage.allCases {
839        print(beverage)
840}
841// coffee
842// tea
843// juice
844\end{swift}
845The syntax used in the examples above marks the enumeration as conforming to the @CaseIterable@ protocol.
846For information about protocols, see Protocols.
847
848\paragraph{Associated Values}
849The examples in the previous section show how the cases of an enumeration are a defined (and typed) value in their own right.
850You can set a constant or variable to Planet.earth, and check for this value later.
851However, it's sometimes useful to be able to store values of other types alongside these case values.
852This additional information is called an associated value, and it varies each time you use that case as a value in your code.
853
854You 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.
855Enumerations similar to these are known as discriminated unions, tagged unions, or variants in other programming languages.
856
857For example, suppose an inventory tracking system needs to track products by two different types of barcode.
858Some products are labeled with 1D barcodes in UPC format, which uses the numbers 0 to 9.
859Each barcode has a number system digit, followed by five manufacturer code digits and five product code digits.
860These are followed by a check digit to verify that the code has been scanned correctly:
861
862Other 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:
863
864It'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.
865
866In Swift, an enumeration to define product barcodes of either type might look like this:
867\begin{swift}
868enum Barcode {
869        case upc(Int, Int, Int, Int)
870        case qrCode(String)
871}
872\end{swift}
873This can be read as:
874\begin{quote}
875"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@."
876\end{quote}
877This 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@.
878
879You can then create new barcodes using either type:
880\begin{swift}
881var productBarcode = Barcode.upc(8, 85909, 51226, 3)
882\end{swift}
883This 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)@.
884
885You can assign the same product a different type of barcode:
886\begin{swift}
887productBarcode = .qrCode("ABCDEFGHIJKLMNOP")
888\end{swift}
889At this point, the original @Barcode.upc@ and its integer values are replaced by the new @Barcode.qrCode@ and its string value.
890Constants 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.
891
892You can check the different barcode types using a switch statement, similar to the example in Matching Enumeration Values with a Switch Statement.
893This time, however, the associated values are extracted as part of the switch statement.
894You 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:
895\begin{swift}
896switch productBarcode {
897case .upc(let numberSystem, let manufacturer, let product, let check):
898        print("UPC: \(numberSystem), \(manufacturer), \(product), \(check).")
899case .qrCode(let productCode):
900        print("QR code: \(productCode).")
901}
902// Prints "QR code: ABCDEFGHIJKLMNOP."
903\end{swift}
904If 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:
905\begin{swift}
906switch productBarcode {
907case let .upc(numberSystem, manufacturer, product, check):
908        print("UPC : \(numberSystem), \(manufacturer), \(product), \(check).")
909case let .qrCode(productCode):
910        print("QR code: \(productCode).")
911}
912// Prints "QR code: ABCDEFGHIJKLMNOP."
913\end{swift}
914
915\paragraph{Raw Values}
916
917The barcode example in Associated Values shows how cases of an enumeration can declare that they store associated values of different types.
918As an alternative to associated values, enumeration cases can come prepopulated with default values (called raw values), which are all of the same type.
919
920Here's an example that stores raw ASCII values alongside named enumeration cases:
921\begin{swift}
922enum ASCIIControlCharacter: Character {
923        case tab = "\t"
924        case lineFeed = "\n"
925        case carriageReturn = "\r"
926}
927\end{swift}
928Here, 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.
929Character values are described in Strings and Characters.
930
931Raw values can be strings, characters, or any of the integer or floating-point number types.
932Each raw value must be unique within its enumeration declaration.
933
934Note
935
936Raw values are not the same as associated values.
937Raw values are set to prepopulated values when you first define the enumeration in your code, like the three ASCII codes above.
938The raw value for a particular enumeration case is always the same.
939Associated 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.
940Implicitly Assigned Raw Values
941
942When 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.
943When you don't, Swift automatically assigns the values for you.
944
945For example, when integers are used for raw values, the implicit value for each case is one more than the previous case.
946If the first case doesn't have a value set, its value is 0.
947
948The enumeration below is a refinement of the earlier Planet enumeration, with integer raw values to represent each planet's order from the sun:
949
950\begin{swift}
951enum Planet: Int {
952        case mercury = 1, venus, earth, mars, jupiter, saturn, uranus, neptune
953}
954\end{swift}
955In the example above, Planet.mercury has an explicit raw value of 1, Planet.venus has an implicit raw value of 2, and so on.
956
957When strings are used for raw values, the implicit value for each case is the text of that case's name.
958
959The enumeration below is a refinement of the earlier CompassPoint enumeration, with string raw values to represent each direction's name:
960\begin{swift}
961enum CompassPoint: String {
962        case north, south, east, west
963}
964\end{swift}
965In the example above, CompassPoint.south has an implicit raw value of "south", and so on.
966
967You access the raw value of an enumeration case with its rawValue property:
968\begin{swift}
969let earthsOrder = Planet.earth.rawValue
970// earthsOrder is 3
971
972let sunsetDirection = CompassPoint.west.rawValue
973// sunsetDirection is "west"
974\end{swift}
975
976\paragraph{Initializing from a Raw Value}
977
978If 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.
979You can use this initializer to try to create a new instance of the enumeration.
980
981This example identifies Uranus from its raw value of 7:
982\begin{swift}
983let possiblePlanet = Planet(rawValue: 7)
984// possiblePlanet is of type Planet? and equals Planet.uranus
985\end{swift}
986Not all possible Int values will find a matching planet, however.
987Because of this, the raw value initializer always returns an optional enumeration case.
988In the example above, possiblePlanet is of type Planet?, or "optional Planet."
989Note
990
991The raw value initializer is a failable initializer, because not every raw value will return an enumeration case.
992For more information, see Failable Initializers.
993
994If you try to find a planet with a position of 11, the optional Planet value returned by the raw value initializer will be nil:
995\begin{swift}
996let positionToFind = 11
997if let somePlanet = Planet(rawValue: positionToFind) {
998        switch somePlanet {
999        case .earth:
1000                print("Mostly harmless")
1001        default:
1002                print("Not a safe place for humans")
1003        }
1004} else {
1005        print("There isn't a planet at position \(positionToFind)")
1006}
1007// Prints "There isn't a planet at position 11"
1008\end{swift}
1009This example uses optional binding to try to access a planet with a raw value of 11.
1010The 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.
1011In this case, it isn't possible to retrieve a planet with a position of 11, and so the else branch is executed instead.
1012
1013\paragraph{Recursive Enumerations}
1014
1015A recursive enumeration is an enumeration that has another instance of the enumeration as the associated value for one or more of the enumeration cases.
1016You indicate that an enumeration case is recursive by writing indirect before it, which tells the compiler to insert the necessary layer of indirection.
1017
1018For example, here is an enumeration that stores simple arithmetic expressions:
1019\begin{swift}
1020enum ArithmeticExpression {
1021        case number(Int)
1022        indirect case addition(ArithmeticExpression, ArithmeticExpression)
1023        indirect case multiplication(ArithmeticExpression, ArithmeticExpression)
1024}
1025\end{swift}
1026You 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:
1027\begin{swift}
1028indirect enum ArithmeticExpression {
1029        case number(Int)
1030        case addition(ArithmeticExpression, ArithmeticExpression)
1031        case multiplication(ArithmeticExpression, ArithmeticExpression)
1032}
1033\end{swift}
1034This enumeration can store three kinds of arithmetic expressions: a plain number, the addition of two expressions, and the multiplication of two expressions.
1035The addition and multiplication cases have associated values that are also arithmetic expressions -- these associated values make it possible to nest expressions.
1036For 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.
1037Because the data is nested, the enumeration used to store the data also needs to support nesting -- this means the enumeration needs to be recursive.
1038The code below shows the ArithmeticExpression recursive enumeration being created for (5 + 4) * 2:
1039\begin{swift}
1040let five = ArithmeticExpression.number(5)
1041let four = ArithmeticExpression.number(4)
1042let sum = ArithmeticExpression.addition(five, four)
1043let product = ArithmeticExpression.multiplication(sum, ArithmeticExpression.number(2))
1044\end{swift}
1045A recursive function is a straightforward way to work with data that has a recursive structure.
1046For example, here's a function that evaluates an arithmetic expression:
1047\begin{swift}
1048func evaluate(_ expression: ArithmeticExpression) -> Int {
1049        switch expression {
1050        case let .number(value):
1051                return value
1052        case let .addition(left, right):
1053                return evaluate(left) + evaluate(right)
1054        case let .multiplication(left, right):
1055                return evaluate(left) * evaluate(right)
1056        }
1057}
1058
1059print(evaluate(product))
1060// Prints "18"
1061\end{swift}
1062This function evaluates a plain number by simply returning the associated value.
1063It 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.
1064
1065
1066\section{Python 3.13}
1067% https://docs.python.org/3/howto/enum.html
1068
1069Python 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.
1070As 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.
1071Nevertheless, an attempt has been made to discuss core enumeration features that come with Python 3.13.
1072
1073A Python enumeration type is a set of ordered scoped identifiers (enumerators) bound to \emph{unique} values.
1074An enumeration is not a basic type;
1075it is a @class@ inheriting from the @Enum@ class, where the enumerators must be explicitly initialized, \eg:
1076\begin{python}
1077class Week(@Enum@): Mon = 1; Tue = 2; Wed = 3; Thu = 4; Fri = 5; Sat = 6; Sun = 7
1078\end{python}
1079and/or explicitly auto initialized, \eg:
1080\begin{python}
1081class Week(Enum): Mon = 1; Tue = 2; Wed = 3; Thu = 10; Fri = @auto()@; Sat = 4; Sun = @auto()@
1082\end{python}
1083where @auto@ increments by 1 from the previous enumerator value.
1084Object initialization and assignment are restricted to the enumerators of this type.
1085An enumerator initialized with same value is an alias and invisible at the enumeration level, \ie the alias it substituted for its aliasee.
1086\begin{python}
1087class Week(Enum): Mon = 1; Tue = 2; Wed = 3; Thu = 10; Fri = @10@; Sat = @10@; Sun = @10@
1088\end{python}
1089Here, the enumeration has only 4 enumerators and 3 aliases.
1090An alias is only visible by dropping down to the @class@ level and asking for class members.
1091@Enum@ only supports equality comparison between enumerator values;
1092the extended class @OrderedEnum@ adds relational operators @<@, @<=@, @>@, and @>=@.
1093
1094There are bidirectional enumeration pseudo-functions for label and value, but there is no concept of access using ordering (position).
1095\begin{cquote}
1096\setlength{\tabcolsep}{15pt}
1097\begin{tabular}{@{}ll@{}}
1098\begin{python}
1099Week.Thu.value == 10;
1100Week.Thu.name == 'Thu';
1101\end{python}
1102&
1103\begin{python}
1104Week( 10 ) == Thu
1105Week['Thu'].value = 10
1106\end{python}
1107\end{tabular}
1108\end{cquote}
1109
1110As an enumeration is a \lstinline[language=python]{class}, its own methods.
1111\begin{python}
1112class Week(Enum):
1113        Mon = 1; Tue = 2; Wed = 3; Thu = 4; Fri = 5; Sat = 6; Sun = 7
1114        $\\@$classmethod
1115        def today(cls, date):
1116                return cls(date.isoweekday())
1117print( "today:", Week.today(date.today()))
1118today: Week.Mon
1119\end{python}
1120The method @today@ retrieves the day of the week and uses it as an index to print out the corresponding label of @Week@.
1121
1122@Flag@ allows combining several members into a single variable:
1123\begin{python}
1124print( repr(WeekF.Sat | WeekF.Sun) )
1125<WeekF.Sun|Sat: 96>
1126\end{python}
1127You can even iterate over a @Flag@ variable:
1128\begin{python}
1129for day in weekend:
1130        print(day)
1131WeekF.Sat
1132WeekF.Sun
1133\end{python}
1134Okay, let's get some chores set up:
1135\begin{python}
1136>>> chores_for_ethan = {
1137...    'feed the cat': Week.MONDAY | Week.WEDNESDAY | Week.FRIDAY,
1138...    'do the dishes': Week.TUESDAY | Week.THURSDAY,
1139...    'answer SO questions': Week.SATURDAY,
1140...    }
1141\end{python}
1142And a function to display the chores for a given day:
1143\begin{python}
1144>>> def show_chores(chores, day):
1145...    for chore, days in chores.items():
1146...        if day in days:
1147...            print(chore)
1148>>> show_chores(chores_for_ethan, Week.SATURDAY)
1149answer SO questions
1150\end{python}
1151Auto incrmenet for @Flag@ is by powers of 2.
1152\begin{python}
1153class WeekF(Flag): Mon = auto(); Tue = auto(); Wed = auto(); Thu = auto(); Fri = auto()\
1154                                                        Sat = auto(); Sun = auto(); Weekend = Sat | Sun
1155for d in WeekF:
1156        print( f"{d.name}: {d.value}", end=" ")
1157Mon: 1 Tue: 2 Wed: 4 Thu: 8 Fri: 16 Sat: 32 Sun: 64 WeekA.Weekend
1158\end{python}
1159
1160\subsection{Programmatic access to enumeration members and their attributes}
1161
1162Sometimes 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).
1163@Enum@ allows such access:
1164\begin{python}
1165print(RGB(1), RGB(3), )
1166RGB.RED RGB.GREEN
1167\end{python}
1168If you want to access enum members by name, use item access:
1169\begin{python}
1170print( RGBa['RED'], RGBa['GREEN'] )
1171RGB.RED RGB.GREEN
1172\end{python}
1173If you have an enum member and need its name or value:
1174\begin{python}
1175member = RGBa.RED
1176print( f"{member.name} {member.value}" )
1177RED 1
1178\end{python}
1179
1180
1181\subsection{Ensuring unique enumeration values}
1182
1183By default, enumerations allow multiple names as aliases for the same value.
1184When this behavior isn't desired, you can use the @unique()@ decorator:
1185\begin{python}
1186from enum import Enum, unique
1187$@$unique
1188class DupVal(Enum): ONE = 1; TWO = 2; THREE = 3; FOUR = 3
1189ValueError: duplicate values found in <enum 'Mistake'>: FOUR -> THREE
1190\end{python}
1191
1192\subsection{Using automatic values}
1193
1194If the exact value is unimportant you can use @auto@:
1195\begin{python}
1196from enum import Enum, auto
1197class RGBa(Enum): RED = auto(); BLUE = auto(); GREEN = auto()
1198\end{python}
1199(Like Golang @iota@.)
1200The values are chosen by @_generate_next_value_()@, which can be overridden:
1201\begin{python}
1202>>> class AutoName(Enum):
1203...     $@$staticmethod
1204...     def _generate_next_value_(name, start, count, last_values):
1205...         return name
1206...
1207>>> class Ordinal(AutoName):
1208...     NORTH = auto()
1209...     SOUTH = auto()
1210...     EAST = auto()
1211...     WEST = auto()
1212...
1213>>> [member.value for member in Ordinal]
1214['NORTH', 'SOUTH', 'EAST', 'WEST']
1215\end{python}
1216Note The @_generate_next_value_()@ method must be defined before any members.
1217
1218\subsection{Iteration}
1219
1220Iterating over the members of an enum does not provide the aliases:
1221\begin{python}
1222>>> list(Shape)
1223[<Shape.SQUARE: 2>, <Shape.DIAMOND: 1>, <Shape.CIRCLE: 3>]
1224>>> list(Week)
1225[<Week.MONDAY: 1>, <Week.TUESDAY: 2>, <Week.WEDNESDAY: 4>, <Week.THURSDAY: 8>,
1226<Week.FRIDAY: 16>, <Week.SATURDAY: 32>, <Week.SUNDAY: 64>]
1227\end{python}
1228Note that the aliases @Shape.ALIAS_FOR_SQUARE@ and @Week.WEEKEND@ aren't shown.
1229
1230The special attribute @__members__@ is a read-only ordered mapping of names to members.
1231It includes all names defined in the enumeration, including the aliases:
1232\begin{python}
1233>>> for name, member in Shape.__members__.items():
1234...     name, member
1235...
1236('SQUARE', <Shape.SQUARE: 2>)
1237('DIAMOND', <Shape.DIAMOND: 1>)
1238('CIRCLE', <Shape.CIRCLE: 3>)
1239('ALIAS_FOR_SQUARE', <Shape.SQUARE: 2>)
1240\end{python}
1241The @__members__@ attribute can be used for detailed programmatic access to the enumeration members.
1242For example, finding all the aliases:
1243\begin{python}
1244>>> [name for name, member in Shape.__members__.items() if member.name != name]
1245['ALIAS_FOR_SQUARE']
1246\end{python}
1247Note: Aliases for flags include values with multiple flags set, such as 3, and no flags set, i.e. 0.
1248
1249\subsection{Comparisons}
1250
1251Enumeration members are compared by identity:
1252\begin{python}
1253>>> Color.RED is Color.RED
1254True
1255>>> Color.RED is Color.BLUE
1256False
1257>>> Color.RED is not Color.BLUE
1258True
1259\end{python}
1260Ordered comparisons between enumeration values are not supported.
1261Enum members are not integers (but see @IntEnum@ below):
1262\begin{python}
1263>>> Color.RED < Color.BLUE
1264Traceback (most recent call last):
1265  File "<stdin>", line 1, in <module>
1266TypeError: '<' not supported between instances of 'Color' and 'Color'
1267\end{python}
1268Equality comparisons are defined though:
1269\begin{python}
1270>>> Color.BLUE == Color.RED
1271False
1272>>> Color.BLUE != Color.RED
1273True
1274>>> Color.BLUE == Color.BLUE
1275True
1276\end{python}
1277Comparisons against non-enumeration values will always compare not equal (again, @IntEnum@ was explicitly designed to behave differently, see below):
1278\begin{python}
1279>>> Color.BLUE == 2
1280False
1281\end{python}
1282
1283Warning: 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.
1284
1285\subsection{Allowed members and attributes of enumerations}
1286
1287Most of the examples above use integers for enumeration values.
1288Using integers is short and handy (and provided by default by the Functional API), but not strictly enforced.
1289In the vast majority of use-cases, one doesn't care what the actual value of an enumeration is.
1290But if the value is important, enumerations can have arbitrary values.
1291
1292Enumerations are Python classes, and can have methods and special methods as usual. If we have this enumeration:
1293\begin{python}
1294>>> class Mood(Enum):
1295...     FUNKY = 1
1296...     HAPPY = 3
1297...
1298...     def describe(self):
1299...         # self is the member here
1300...         return self.name, self.value
1301...
1302...     def __str__(self):
1303...         return 'my custom str! {0}'.format(self.value)
1304...
1305...     $@$classmethod
1306...
1307...     def favorite_mood(cls):
1308...         # cls here is the enumeration
1309...         return cls.HAPPY
1310...
1311\end{python}
1312Then:
1313\begin{python}
1314>>> Mood.favorite_mood()
1315<Mood.HAPPY: 3>
1316>>> Mood.HAPPY.describe()
1317('HAPPY', 3)
1318>>> str(Mood.FUNKY)
1319'my custom str! 1'
1320\end{python}
1321The 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;
1322all 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_@.
1323
1324Note: if your enumeration defines @__new__()@ and/or @__init__()@, any value(s) given to the enum member will be passed into those methods.
1325See Planet for an example.
1326
1327Note: The @__new__()@ method, if defined, is used during creation of the Enum members;
1328it is then replaced by Enum's @__new__()@ which is used after class creation for lookup of existing members.
1329See When to use @__new__()@ vs. @__init__()@ for more details.
1330
1331\subsection{Restricted Enum subclassing}
1332
1333A new @Enum@ class must have one base enum class, up to one concrete data type, and as many object-based mixin classes as needed.
1334The order of these base classes is:
1335\begin{python}
1336class EnumName([mix-in, ...,] [data-type,] base-enum):
1337        pass
1338\end{python}
1339Also, subclassing an enumeration is allowed only if the enumeration does not define any members.
1340So this is forbidden:
1341\begin{python}
1342>>> class MoreColor(Color):
1343...     PINK = 17
1344...
1345Traceback (most recent call last):
1346...
1347TypeError: <enum 'MoreColor'> cannot extend <enum 'Color'>
1348\end{python}
1349But this is allowed:
1350\begin{python}
1351>>> class Foo(Enum):
1352...     def some_behavior(self):
1353...         pass
1354...
1355>>> class Bar(Foo):
1356...     HAPPY = 1
1357...     SAD = 2
1358...
1359\end{python}
1360Allowing subclassing of enums that define members would lead to a violation of some important invariants of types and instances.
1361On the other hand, it makes sense to allow sharing some common behavior between a group of enumerations. (See OrderedEnum for an example.)
1362
1363\subsection{Dataclass support}
1364
1365When inheriting from a @dataclass@, the @__repr__()@ omits the inherited class' name.
1366For example:
1367\begin{python}
1368>>> from dataclasses import dataclass, field
1369>>> $@$dataclass
1370... class CreatureDataMixin:
1371...     size: str
1372...     legs: int
1373...     tail: bool = field(repr=False, default=True)
1374...
1375>>> class Creature(CreatureDataMixin, Enum):
1376...     BEETLE = 'small', 6
1377...     DOG = 'medium', 4
1378...
1379>>> Creature.DOG
1380<Creature.DOG: size='medium', legs=4>
1381\end{python}
1382Use the @dataclass()@ argument repr=False to use the standard @repr()@.
1383
1384Changed in version 3.12: Only the dataclass fields are shown in the value area, not the dataclass' name.
1385
1386\subsection{Pickling}
1387
1388Enumerations can be pickled and unpickled:
1389\begin{python}
1390>>> from test.test_enum import Fruit
1391>>> from pickle import dumps, loads
1392>>> Fruit.TOMATO is loads(dumps(Fruit.TOMATO))
1393True
1394\end{python}
1395The 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.
1396
1397Note: With pickle protocol version 4 it is possible to easily pickle enums nested in other classes.
1398
1399It is possible to modify how enum members are pickled/unpickled by defining @__reduce_ex__()@ in the enumeration class.
1400The default method is by-value, but enums with complicated values may want to use by-name:
1401\begin{python}
1402>>> import enum
1403>>> class MyEnum(enum.Enum):
1404...     __reduce_ex__ = enum.pickle_by_enum_name
1405\end{python}
1406Note: Using by-name for flags is not recommended, as unnamed aliases will not unpickle.
1407
1408\subsection{Functional API}
1409
1410The @Enum@ class is callable, providing the following functional API:
1411\begin{python}
1412>>> Animal = Enum('Animal', 'ANT BEE CAT DOG')
1413>>> Animal
1414<enum 'Animal'>
1415>>> Animal.ANT
1416<Animal.ANT: 1>
1417>>> list(Animal)
1418[<Animal.ANT: 1>, <Animal.BEE: 2>, <Animal.CAT: 3>, <Animal.DOG: 4>]
1419\end{python}
1420The semantics of this API resemble @namedtuple@.
1421The first argument of the call to @Enum@ is the name of the enumeration.
1422
1423The second argument is the source of enumeration member names.
1424It 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.
1425The last two options enable assigning arbitrary values to enumerations;
1426the others auto-assign increasing integers starting with 1 (use the @start@ parameter to specify a different starting value).
1427A new class derived from @Enum@ is returned.
1428In other words, the above assignment to Animal is equivalent to:
1429\begin{python}
1430>>> class Animal(Enum):
1431...     ANT = 1
1432...     BEE = 2
1433...     CAT = 3
1434...     DOG = 4
1435...
1436\end{python}
1437The 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@.
1438
1439Pickling 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).
1440The solution is to specify the module name explicitly as follows:
1441\begin{python}
1442>>> Animal = Enum('Animal', 'ANT BEE CAT DOG', module=__name__)
1443\end{python}
1444Warning: 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.
1445
1446The 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.
1447For example, if the class was made available in class SomeData in the global scope:
1448\begin{python}
1449>>> Animal = Enum('Animal', 'ANT BEE CAT DOG', qualname='SomeData.Animal')
1450\end{python}
1451The complete signature is:
1452\begin{python}
1453Enum(
1454        value='NewEnumName',
1455        names=<...>,
1456        *,
1457        module='...',
1458        qualname='...',
1459        type=<mixed-in class>,
1460        start=1,
1461        )
1462\end{python}
1463\begin{itemize}
1464\item
1465@value@: What the new enum class will record as its name.
1466\item
1467@names@: The enum members.
1468This can be a whitespace- or comma-separated string (values will start at 1 unless otherwise specified):
1469\begin{python}
1470'RED GREEN BLUE' | 'RED,GREEN,BLUE' | 'RED, GREEN, BLUE'
1471\end{python}
1472or an iterator of names:
1473\begin{python}
1474['RED', 'GREEN', 'BLUE']
1475\end{python}
1476or an iterator of (name, value) pairs:
1477\begin{python}
1478[('CYAN', 4), ('MAGENTA', 5), ('YELLOW', 6)]
1479\end{python}
1480or a mapping:
1481\begin{python}
1482{'CHARTREUSE': 7, 'SEA_GREEN': 11, 'ROSEMARY': 42}
1483\end{python}
1484\item
1485module: name of module where new enum class can be found.
1486\item
1487@qualname@: where in module new enum class can be found.
1488\item
1489@type@: type to mix in to new enum class.
1490\item
1491@start@: number to start counting at if only names are passed in.
1492\end{itemize}
1493Changed in version 3.5: The start parameter was added.
1494
1495\subsection{Derived Enumerations}
1496
1497\subsection{IntEnum}
1498
1499The first variation of @Enum@ that is provided is also a subclass of @int@.
1500Members of an @IntEnum@ can be compared to integers;
1501by extension, integer enumerations of different types can also be compared to each other:
1502\begin{python}
1503>>> from enum import IntEnum
1504>>> class Shape(IntEnum):
1505...     CIRCLE = 1
1506...     SQUARE = 2
1507...
1508>>> class Request(IntEnum):
1509...     POST = 1
1510...     GET = 2
1511...
1512>>> Shape == 1
1513False
1514>>> Shape.CIRCLE == 1
1515True
1516>>> Shape.CIRCLE == Request.POST
1517True
1518\end{python}
1519However, they still can't be compared to standard @Enum@ enumerations:
1520\begin{python}
1521>>> class Shape(IntEnum):
1522...     CIRCLE = 1
1523...     SQUARE = 2
1524...
1525>>> class Color(Enum):
1526...     RED = 1
1527...     GREEN = 2
1528...
1529>>> Shape.CIRCLE == Color.RED
1530False
1531\end{python}
1532@IntEnum@ values behave like integers in other ways you'd expect:
1533\begin{python}
1534>>> int(Shape.CIRCLE)
15351
1536>>> ['a', 'b', 'c'][Shape.CIRCLE]
1537'b'
1538>>> [i for i in range(Shape.SQUARE)]
1539[0, 1]
1540\end{python}
1541
1542\subsection{StrEnum}
1543
1544The second variation of @Enum@ that is provided is also a subclass of @str@.
1545Members of a @StrEnum@ can be compared to strings;
1546by extension, string enumerations of different types can also be compared to each other.
1547
1548New in version 3.11.
1549
1550\subsection{IntFlag}
1551
1552The next variation of @Enum@ provided, @IntFlag@, is also based on @int@.
1553The difference being @IntFlag@ members can be combined using the bitwise operators (@&, |, ^, ~@) and the result is still an @IntFlag@ member, if possible.
1554Like @IntEnum@, @IntFlag@ members are also integers and can be used wherever an int is used.
1555
1556Note: Any operation on an IntFlag member besides the bit-wise operations will lose the @IntFlag@ membership.
1557
1558Bit-wise operations that result in invalid @IntFlag@ values will lose the @IntFlag@ membership.
1559See @FlagBoundary@ for details.
1560
1561New in version 3.6.
1562
1563Changed in version 3.11.
1564
1565Sample @IntFlag@ class:
1566\begin{python}
1567>>> from enum import IntFlag
1568>>> class Perm(IntFlag):
1569...     R = 4
1570...     W = 2
1571...     X = 1
1572...
1573>>> Perm.R | Perm.W
1574<Perm.R|W: 6>
1575>>> Perm.R + Perm.W
15766
1577>>> RW = Perm.R | Perm.W
1578>>> Perm.R in RW
1579True
1580\end{python}
1581It is also possible to name the combinations:
1582\begin{python}
1583>>> class Perm(IntFlag):
1584...     R = 4
1585...     W = 2
1586...     X = 1
1587...     RWX = 7
1588...
1589>>> Perm.RWX
1590<Perm.RWX: 7>
1591>>> ~Perm.RWX
1592<Perm: 0>
1593>>> Perm(7)
1594<Perm.RWX: 7>
1595\end{python}
1596Note: Named combinations are considered aliases. Aliases do not show up during iteration, but can be returned from by-value lookups.
1597
1598Changed in version 3.11.
1599
1600Another important difference between @IntFlag@ and @Enum@ is that if no flags are set (the value is 0), its boolean evaluation is @False@:
1601\begin{python}
1602>>> Perm.R & Perm.X
1603<Perm: 0>
1604>>> bool(Perm.R & Perm.X)
1605False
1606\end{python}
1607Because @IntFlag@ members are also subclasses of int they can be combined with them (but may lose @IntFlag@ membership:
1608\begin{python}
1609>>> Perm.X | 4
1610<Perm.R|X: 5>
1611
1612>>> Perm.X + 8
16139
1614\end{python}
1615Note: The negation operator, @~@, always returns an @IntFlag@ member with a positive value:
1616\begin{python}
1617>>> (~Perm.X).value == (Perm.R|Perm.W).value == 6
1618True
1619\end{python}
1620@IntFlag@ members can also be iterated over:
1621\begin{python}
1622>>> list(RW)
1623[<Perm.R: 4>, <Perm.W: 2>]
1624\end{python}
1625New in version 3.11.
1626
1627\subsection{Flag}
1628
1629The last variation is @Flag@.
1630Like @IntFlag@, @Flag@ members can be combined using the bitwise operators (@&, |, ^, ~@).
1631Unlike @IntFlag@, they cannot be combined with, nor compared against, any other @Flag@ enumeration, nor @int@.
1632While it is possible to specify the values directly it is recommended to use @auto@ as the value and let @Flag@ select an appropriate value.
1633
1634New in version 3.6.
1635
1636Like @IntFlag@, if a combination of @Flag@ members results in no flags being set, the boolean evaluation is @False@:
1637\begin{python}
1638>>> from enum import Flag, auto
1639>>> class Color(Flag):
1640...     RED = auto()
1641...     BLUE = auto()
1642...     GREEN = auto()
1643...
1644>>> Color.RED & Color.GREEN
1645<Color: 0>
1646>>> bool(Color.RED & Color.GREEN)
1647False
1648\end{python}
1649Individual flags should have values that are powers of two (1, 2, 4, 8, ...), while combinations of flags will not:
1650\begin{python}
1651>>> class Color(Flag):
1652...     RED = auto()
1653...     BLUE = auto()
1654...     GREEN = auto()
1655...     WHITE = RED | BLUE | GREEN
1656...
1657>>> Color.WHITE
1658<Color.WHITE: 7>
1659\end{python}
1660Giving a name to the ``no flags set'' condition does not change its boolean value:
1661\begin{python}
1662>>> class Color(Flag):
1663...     BLACK = 0
1664...     RED = auto()
1665...     BLUE = auto()
1666...     GREEN = auto()
1667...
1668>>> Color.BLACK
1669<Color.BLACK: 0>
1670>>> bool(Color.BLACK)
1671False
1672\end{python}
1673@Flag@ members can also be iterated over:
1674\begin{python}
1675>>> purple = Color.RED | Color.BLUE
1676>>> list(purple)
1677[<Color.RED: 1>, <Color.BLUE: 2>]
1678\end{python}
1679New in version 3.11.
1680
1681Note: 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).
1682@IntEnum@ and @IntFlag@ should be used only in cases where @Enum@ and @Flag@ will not do;
1683for example, when integer constants are replaced with enumerations, or for interoperability with other systems.
1684
1685\subsection{Others}
1686
1687While @IntEnum@ is part of the enum module, it would be very simple to implement independently:
1688\begin{python}
1689class IntEnum(int, Enum):
1690        pass
1691\end{python}
1692This demonstrates how similar derived enumerations can be defined;
1693for example a @FloatEnum@ that mixes in float instead of @int@.
1694
1695Some rules:
1696\begin{itemize}
1697\item
1698When subclassing @Enum@, mix-in types must appear before @Enum@ itself in the sequence of bases, as in the @IntEnum@ example above.
1699\item
1700Mix-in types must be subclassable.
1701For example, @bool@ and @range@ are not subclassable and will throw an error during Enum creation if used as the mix-in type.
1702\item
1703While @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.
1704This restriction does not apply to mix-ins which only add methods and don't specify another type.
1705\item
1706When 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.
1707\item
1708A data type is a mixin that defines @__new__()@, or a @dataclass@
1709\item
1710\%-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.
1711\item
1712Formatted string literals, @str.format()@, and format() will use the enum's @__str__()@ method.
1713\end{itemize}
1714Note: 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.
1715
1716\subsection{When to use \lstinline{__new__()} vs. \lstinline{__init__()}}
1717
1718@__new__()@ must be used whenever you want to customize the actual value of the @Enum@ member.
1719Any other modifications may go in either @__new__()@ or @__init__()@, with @__init__()@ being preferred.
1720
1721For example, if you want to pass several items to the constructor, but only want one of them to be the value:
1722\begin{python}
1723>>> class Coordinate(bytes, Enum):
1724...     """
1725...     Coordinate with binary codes that can be indexed by the int code.
1726...     """
1727...     def __new__(cls, value, label, unit):
1728...         obj = bytes.__new__(cls, [value])
1729...         obj._value_ = value
1730...         obj.label = label
1731...         obj.unit = unit
1732...         return obj
1733...     PX = (0, 'P.X', 'km')
1734...     PY = (1, 'P.Y', 'km')
1735...     VX = (2, 'V.X', 'km/s')
1736...     VY = (3, 'V.Y', 'km/s')
1737
1738>>> print(Coordinate['PY'])
1739Coordinate.PY
1740
1741>>> print(Coordinate(3))
1742Coordinate.VY
1743\end{python}
1744Warning: Do not call @super().__new__()@, as the lookup-only @__new__@ is the one that is found; instead, use the data type directly.
1745
1746\subsection{Finer Points}
1747
1748Supported @__dunder__@ names
1749
1750@__members__@ is a read-only ordered mapping of member\_name:member items. It is only available on the class.
1751
1752@__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.
1753Supported @_sunder_@ names
1754\begin{itemize}
1755\item
1756@_name_@ -- name of the member
1757\item
1758@_value_@ -- value of the member; can be set / modified in @__new__@
1759\item
1760@_missing_@ -- a lookup function used when a value is not found; may be overridden
1761\item
1762@_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
1763\item
1764@_order_@ -- used in Python 2/3 code to ensure member order is consistent (class attribute, removed during class creation)
1765\item
1766@_generate_@next@_value_@ -- used by the Functional API and by @auto@ to get an appropriate value for an enum member; may be overridden
1767\end{itemize}
1768Note: For standard @Enum@ classes the next value chosen is the last value seen incremented by one.
1769
1770For @Flag@ classes the next value chosen will be the next highest power-of-two, regardless of the last value seen.
1771
1772New in version 3.6: @_missing_@, @_order_@, @_generate_@next@_value_@
1773
1774New in version 3.7: @_ignore_@
1775
1776To help keep Python 2 / Python 3 code in sync an @_order_@ attribute can be provided.
1777It will be checked against the actual order of the enumeration and raise an error if the two do not match:
1778\begin{python}
1779>>> class Color(Enum):
1780...     _order_ = 'RED GREEN BLUE'
1781...     RED = 1
1782...     BLUE = 3
1783...     GREEN = 2
1784...
1785Traceback (most recent call last):
1786...
1787TypeError: member order does not match _order_:
1788  ['RED', 'BLUE', 'GREEN']
1789  ['RED', 'GREEN', 'BLUE']
1790\end{python}
1791Note: In Python 2 code the @_order_@ attribute is necessary as definition order is lost before it can be recorded.
1792
1793\subsection{\lstinline{_Private__names}}
1794
1795Private names are not converted to enum members, but remain normal attributes.
1796
1797Changed in version 3.11.
1798
1799\subsection{\lstinline{Enum} member type}
1800
1801@Enum@ members are instances of their enum class, and are normally accessed as @EnumClass.member@.
1802In certain situations, such as writing custom enum behavior, being able to access one member directly from another is useful, and is supported;
1803however, in order to avoid name clashes between member names and attributes/methods from mixed-in classes, upper-case names are strongly recommended.
1804
1805Changed in version 3.5.
1806
1807\subsection{Creating members that are mixed with other data types}
1808
1809When 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:
1810\begin{python}
1811>>> class MyEnum(IntEnum):      # help(int) -> int(x, base=10) -> integer
1812...     example = '11', 16      # so x='11' and base=16
1813...
1814MyEnum.example.value        # and hex(11) is...
181517
1816\end{python}
1817
1818\subsection{\lstinline{Boolean} value of \lstinline{Enum} classes and members}
1819
1820Enum classes that are mixed with non-@Enum@ types (such as @int@, @str@, etc.) are evaluated according to the mixed-in type's rules;
1821otherwise, all members evaluate as @True@.
1822To make your own enum's boolean evaluation depend on the member's value add the following to your class:
1823\begin{python}
1824def __bool__(self):
1825        return bool(self.value)
1826\end{python}
1827Plain @Enum@ classes always evaluate as @True@.
1828
1829\subsection{\lstinline{Enum} classes with methods}
1830
1831If 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:
1832\begin{python}
1833>>> dir(Planet)                         
1834['EARTH', 'JUPITER', 'MARS', 'MERCURY', 'NEPTUNE', 'SATURN', 'URANUS', 'VENUS',
1835 '__class__', '__doc__', '__members__', '__module__']
1836>>> dir(Planet.EARTH)                   
1837['__class__', '__doc__', '__module__', 'mass', 'name', 'radius', 'surface_gravity', 'value']
1838\end{python}
1839
1840\subsection{Combining members of \lstinline{Flag}}
1841
1842Iterating over a combination of @Flag@ members will only return the members that are comprised of a single bit:
1843\begin{python}
1844>>> class Color(Flag):
1845...     RED = auto()
1846...     GREEN = auto()
1847...     BLUE = auto()
1848...     MAGENTA = RED | BLUE
1849...     YELLOW = RED | GREEN
1850...     CYAN = GREEN | BLUE
1851...
1852>>> Color(3)  # named combination
1853<Color.YELLOW: 3>
1854>>> Color(7)      # not named combination
1855<Color.RED|GREEN|BLUE: 7>
1856\end{python}
1857
1858\subsection{\lstinline{Flag} and \lstinline{IntFlag} minutia}
1859
1860Using the following snippet for our examples:
1861\begin{python}
1862>>> class Color(IntFlag):
1863...     BLACK = 0
1864...     RED = 1
1865...     GREEN = 2
1866...     BLUE = 4
1867...     PURPLE = RED | BLUE
1868...     WHITE = RED | GREEN | BLUE
1869...
1870\end{python}
1871the following are true:
1872\begin{itemize}
1873\item
1874single-bit flags are canonical
1875\item
1876multi-bit and zero-bit flags are aliases
1877\item
1878only canonical flags are returned during iteration:
1879\begin{python}
1880>>> list(Color.WHITE)
1881[<Color.RED: 1>, <Color.GREEN: 2>, <Color.BLUE: 4>]
1882\end{python}
1883negating a flag or flag set returns a new flag/flag set with the corresponding positive integer value:
1884\begin{python}
1885>>> Color.BLUE
1886<Color.BLUE: 4>
1887
1888>>> ~Color.BLUE
1889<Color.RED|GREEN: 3>
1890\end{python}
1891\item
1892names of pseudo-flags are constructed from their members' names:
1893\begin{python}
1894>>> (Color.RED | Color.GREEN).name
1895'RED|GREEN'
1896\end{python}
1897\item
1898multi-bit flags, aka aliases, can be returned from operations:
1899\begin{python}
1900>>> Color.RED | Color.BLUE
1901<Color.PURPLE: 5>
1902
1903>>> Color(7)  # or Color(-1)
1904<Color.WHITE: 7>
1905
1906>>> Color(0)
1907<Color.BLACK: 0>
1908\end{python}
1909\item
1910membership / containment checking: zero-valued flags are always considered to be contained:
1911\begin{python}
1912>>> Color.BLACK in Color.WHITE
1913True
1914\end{python}
1915otherwise, only if all bits of one flag are in the other flag will True be returned:
1916\begin{python}
1917>>> Color.PURPLE in Color.WHITE
1918True
1919
1920>>> Color.GREEN in Color.PURPLE
1921False
1922\end{python}
1923\end{itemize}
1924There is a new boundary mechanism that controls how out-of-range / invalid bits are handled: @STRICT@, @CONFORM@, @EJECT@, and @KEEP@:
1925\begin{itemize}
1926\item
1927@STRICT@ --> raises an exception when presented with invalid values
1928\item
1929@CONFORM@ --> discards any invalid bits
1930\item
1931@EJECT@ --> lose Flag status and become a normal int with the given value
1932\item
1933@KEEP@ --> keep the extra bits
1934\begin{itemize}
1935\item
1936keeps Flag status and extra bits
1937\item
1938extra bits do not show up in iteration
1939\item
1940extra bits do show up in repr() and str()
1941\end{itemize}
1942\end{itemize}
1943The 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).
1944
1945\section{How are Enums and Flags different?}
1946
1947Enums have a custom metaclass that affects many aspects of both derived @Enum@ classes and their instances (members).
1948
1949\subsection{Enum Classes}
1950
1951The @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@.
1952@EnumType@ is responsible for ensuring that various other methods on the final @Enum@ class are correct (such as @__new__()@, @__getnewargs__()@, @__str__()@ and @__repr__()@).
1953
1954\subsection{Flag Classes}
1955
1956Flags 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.
1957So, 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.
1958
1959\subsection{Enum Members (aka instances)}
1960
1961The most interesting thing about enum members is that they are singletons.
1962@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.
1963
1964\subsection{Flag Members}
1965
1966Flag members can be iterated over just like the @Flag@ class, and only the canonical members will be returned.
1967For example:
1968\begin{python}
1969>>> list(Color)
1970[<Color.RED: 1>, <Color.GREEN: 2>, <Color.BLUE: 4>]
1971\end{python}
1972(Note that BLACK, PURPLE, and WHITE do not show up.)
1973
1974Inverting a flag member returns the corresponding positive value, rather than a negative value -- for example:
1975\begin{python}
1976>>> ~Color.RED
1977<Color.GREEN|BLUE: 6>
1978\end{python}
1979Flag members have a length corresponding to the number of power-of-two values they contain. For example:
1980\begin{python}
1981>>> len(Color.PURPLE)
19822
1983\end{python}
1984
1985\subsection{Enum Cookbook}
1986
1987While @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.
1988
1989\subsection{Omitting values}
1990
1991In 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:
1992\begin{itemize}
1993\item
1994use instances of auto for the value
1995\item
1996use instances of object as the value
1997\item
1998use a descriptive string as the value
1999\item
2000use a tuple as the value and a custom @__new__()@ to replace the tuple with an @int@ value
2001\end{itemize}
2002Using 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.
2003
2004\subsection{Using \lstinline{auto}}
2005
2006Using @auto@ would look like:
2007\begin{python}
2008>>> class Color(Enum):
2009...     RED = auto()
2010...     BLUE = auto()
2011...     GREEN = auto()
2012...
2013>>> Color.GREEN
2014<Color.GREEN: 3>
2015\end{python}
2016
2017\subsection{Using \lstinline{object}}
2018
2019Using @object@ would look like:
2020\begin{python}
2021>>> class Color(Enum):
2022...     RED = object()
2023...     GREEN = object()
2024...     BLUE = object()
2025...
2026>>> Color.GREEN                         
2027<Color.GREEN: <object object at 0x...>>
2028\end{python}
2029This is also a good example of why you might want to write your own @__repr__()@:
2030\begin{python}
2031>>> class Color(Enum):
2032...     RED = object()
2033...     GREEN = object()
2034...     BLUE = object()
2035...     def __repr__(self):
2036...         return "<%s.%s>" % (self.__class__.__name__, self._name_)
2037...
2038>>> Color.GREEN
2039<Color.GREEN>
2040\end{python}
2041
2042\subsection{Using a descriptive string}
2043
2044Using a string as the value would look like:
2045\begin{python}
2046>>> class Color(Enum):
2047...     RED = 'stop'
2048...     GREEN = 'go'
2049...     BLUE = 'too fast!'
2050...
2051>>> Color.GREEN
2052<Color.GREEN: 'go'>
2053\end{python}
2054
2055\subsection{Using a custom \lstinline{__new__()}}
2056
2057Using an auto-numbering @__new__()@ would look like:
2058\begin{python}
2059>>> class AutoNumber(Enum):
2060...     def __new__(cls):
2061...         value = len(cls.__members__) + 1
2062...         obj = object.__new__(cls)
2063...         obj._value_ = value
2064...         return obj
2065...
2066>>> class Color(AutoNumber):
2067...     RED = ()
2068...     GREEN = ()
2069...     BLUE = ()
2070...
2071>>> Color.GREEN
2072<Color.GREEN: 2>
2073\end{python}
2074To make a more general purpose @AutoNumber@, add @*args@ to the signature:
2075\begin{python}
2076>>> class AutoNumber(Enum):
2077...     def __new__(cls, *args):      # this is the only change from above
2078...         value = len(cls.__members__) + 1
2079...         obj = object.__new__(cls)
2080...         obj._value_ = value
2081...         return obj
2082\end{python}
2083Then when you inherit from @AutoNumber@ you can write your own @__init__@ to handle any extra arguments:
2084\begin{python}
2085>>> class Swatch(AutoNumber):
2086...     def __init__(self, pantone='unknown'):
2087...         self.pantone = pantone
2088...     AUBURN = '3497'
2089...     SEA_GREEN = '1246'
2090...     BLEACHED_CORAL = () # New color, no Pantone code yet!
2091...
2092>>> Swatch.SEA_GREEN
2093<Swatch.SEA_GREEN: 2>
2094>>> Swatch.SEA_GREEN.pantone
2095'1246'
2096>>> Swatch.BLEACHED_CORAL.pantone
2097'unknown'
2098\end{python}
2099Note: The @__new__()@ method, if defined, is used during creation of the Enum members;
2100it is then replaced by Enum's @__new__()@ which is used after class creation for lookup of existing members.
2101
2102Warning: Do not call @super().__new__()@, as the lookup-only @__new__@ is the one that is found;
2103instead, use the data type directly -- e.g.:
2104\begin{python}
2105obj = int.__new__(cls, value)
2106\end{python}
2107
2108\subsection{OrderedEnum}
2109
2110An ordered enumeration that is not based on @IntEnum@ and so maintains the normal @Enum@ invariants (such as not being comparable to other enumerations):
2111\begin{python}
2112>>> class OrderedEnum(Enum):
2113...     def __ge__(self, other):
2114...         if self.__class__ is other.__class__:
2115...             return self.value >= other.value
2116...         return NotImplemented
2117...     def __gt__(self, other):
2118...         if self.__class__ is other.__class__:
2119...             return self.value > other.value
2120...         return NotImplemented
2121...     def __le__(self, other):
2122...         if self.__class__ is other.__class__:
2123...             return self.value <= other.value
2124...         return NotImplemented
2125...     def __lt__(self, other):
2126...         if self.__class__ is other.__class__:
2127...             return self.value < other.value
2128...         return NotImplemented
2129...
2130>>> class Grade(OrderedEnum):
2131...     A = 5
2132...     B = 4
2133...     C = 3
2134...     D = 2
2135...     F = 1
2136>>> Grade.C < Grade.A
2137True
2138\end{python}
2139
2140\subsection{DuplicateFreeEnum}
2141
2142Raises an error if a duplicate member value is found instead of creating an alias:
2143\begin{python}
2144>>> class DuplicateFreeEnum(Enum):
2145...     def __init__(self, *args):
2146...         cls = self.__class__
2147...         if any(self.value == e.value for e in cls):
2148...             a = self.name
2149...             e = cls(self.value).name
2150...             raise ValueError(
2151...                 "aliases not allowed in DuplicateFreeEnum:  %r --> %r"
2152...                 % (a, e))
2153>>> class Color(DuplicateFreeEnum):
2154...     RED = 1
2155...     GREEN = 2
2156...     BLUE = 3
2157...     GRENE = 2
2158...
2159Traceback (most recent call last):
2160  ...
2161ValueError: aliases not allowed in DuplicateFreeEnum:  'GRENE' --> 'GREEN'
2162\end{python}
2163Note: This is a useful example for subclassing Enum to add or change other behaviors as well as disallowing aliases.
2164If the only desired change is disallowing aliases, the @unique()@ decorator can be used instead.
2165
2166\subsection{Planet}
2167
2168If @__new__()@ or @__init__()@ is defined, the value of the enum member will be passed to those methods:
2169\begin{figure}
2170\begin{python}
2171from enum import Enum
2172class Planet(Enum):
2173        MERCURY = ( 3.303E23, 2.4397E6 )
2174        VENUS       = ( 4.869E24, 6.0518E6 )
2175        EARTH       = (5.976E24, 6.37814E6)
2176        MARS         = (6.421E23, 3.3972E6)
2177        JUPITER    = (1.9E27,   7.1492E7)
2178        SATURN     = (5.688E26, 6.0268E7)
2179        URANUS    = (8.686E25, 2.5559E7)
2180        NEPTUNE  = (1.024E26, 2.4746E7)
2181        def __init__( self, mass, radius ):
2182                self.mass = mass                # in kilograms
2183                self.radius = radius    # in meters
2184        def surface_gravity( self ):
2185                # universal gravitational constant  (m3 kg-1 s-2)
2186                G = 6.67300E-11
2187                return G * self.mass / (self.radius * self.radius)
2188for p in Planet:
2189        print( f"{p.name}: {p.value}" )
2190
2191MERCURY: (3.303e+23, 2439700.0)
2192VENUS: (4.869e+24, 6051800.0)
2193EARTH: (5.976e+24, 6378140.0)
2194MARS: (6.421e+23, 3397200.0)
2195JUPITER: (1.9e+27, 71492000.0)
2196SATURN: (5.688e+26, 60268000.0)
2197URANUS: (8.686e+25, 25559000.0)
2198NEPTUNE: (1.024e+26, 24746000.0)
2199\end{python}
2200\caption{Python Planet Example}
2201\label{f:PythonPlanetExample}
2202\end{figure}
2203
2204
2205\subsection{TimePeriod}
2206
2207An example to show the @_ignore_@ attribute in use:
2208\begin{python}
2209>>> from datetime import timedelta
2210>>> class Period(timedelta, Enum):
2211...     "different lengths of time"
2212...     _ignore_ = 'Period i'
2213...     Period = vars()
2214...     for i in range(367):
2215...         Period['day_%d' % i] = i
2216...
2217>>> list(Period)[:2]
2218[<Period.day_0: datetime.timedelta(0)>, <Period.day_1: datetime.timedelta(days=1)>]
2219>>> list(Period)[-2:]
2220[<Period.day_365: datetime.timedelta(days=365)>, <Period.day_366: datetime.timedelta(days=366)>]
2221\end{python}
2222
2223\subsection{Subclassing EnumType}
2224
2225While 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.
2226
2227
2228\section{OCaml}
2229
2230% https://ocaml.org/docs/basic-data-types#enumerated-data-types
2231% https://dev.realworldocaml.org/runtime-memory-layout.html
2232
2233OCaml provides a variant (union) type, where multiple heterogeneously-typed objects share the same storage.
2234The simplest form of the variant type is a list of nullary datatype constructors, which is like an unscoped, opaque enumeration.
2235
2236OCaml provides a variant (union) type, which is an aggregation of heterogeneous types.
2237A basic variant is a list of nullary datatype constructors, which is like an unscoped, opaque enumeration.
2238\begin{ocaml}
2239type week = Mon | Tue | Wed | Thu | Fri | Sat | Sun
2240let day : week @= Mon@                          $\C{(* bind *)}$
2241let take_class( d : week ) =
2242        @match@ d with                                          $\C{(* matching *)}$
2243                Mon | Wed -> Printf.printf "CS442\n" |
2244                Tue | Thu -> Printf.printf "CS343\n" |
2245                Fri -> Printf.printf "Tutorial\n" |
2246                _ -> Printf.printf "Take a break\n"
2247let _ = take_class( Mon );
2248@CS442@
2249\end{ocaml}
2250The 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.
2251After compilation, variant names are mapped to an opague ascending intergral type discriminants, starting from 0.
2252Here, function @take_class@ has a @week@ parameter, and returns @"CS442"@, if the week value is @Mon@ or @Wed@, @"CS343"@, if the value is @Tue@ or @Thu@, and @"Tutorial"@ for @Fri@.
2253The ``@_@'' is a wildcard matching any @week@ value, so the function returns @"Take a break"@ for values @Sat@ or @Sun@, which are not matched by the previous cases.
2254Since the variant has no type, it has a \newterm{0-arity constructor}, \ie no parameters.
2255Because @week@ is a union of values @Mon@ to @Sun@, it is a \newterm{union type} in turns of the functional-programming paradigm.
2256
2257Each variant can have an associated heterogeneous type, with an n-ary constructor for creating a corresponding value.
2258\begin{ocaml}
2259type colour = Red | Green of @string@ | Blue of @int * float@
2260\end{ocaml}
2261A variant with parameter is stored in a memory block, prefixed by an int tag and has its parameters stores as words in the block.
2262@colour@ is a summation of a nullary type, a unary product type of @string@, and a cross product of @int@ and @float@.
2263(Mathematically, a @Blue@ value is a Cartesian product of the types @int@ type and @float@.)
2264Hence, a variant type creates a sum of product of different types.
2265\begin{ocaml}
2266let c = Red                                                             $\C{(* 0-ary constructor, set tag *)}$
2267let _ = match c with Red -> Printf.printf "Red, "
2268let c = Green( "abc" )                                  $\C{(* 1-ary constructor, set tag *)}$
2269let _ = match c with Green g -> Printf.printf "%s, " g
2270let c = Blue( 1, 1.5 )                                  $\C{(* 2-ary constructor, set tag *)}$
2271let _ = match c with Blue( i, f ) -> Printf.printf "%d %g\n" i f
2272@Red, abc, 1 1.5@
2273\end{ocaml}
2274
2275A variant type can have a recursive definition.
2276\begin{ocaml}
2277type @stringList@ = Empty | Pair of string * @stringList@
2278\end{ocaml}
2279which is a recursive sum of product of types, called an \newterm{algebraic data-type}.
2280A recursive function is often used to pattern match against a recursive variant type.
2281\begin{ocaml}
2282let rec @len_of_string_list@( list : stringList ): int =
2283        match list with
2284                Empty -> 0 |
2285                Pair( _ , r ) -> 1 + @len_of_string_list@ r
2286\end{ocaml}
2287Here, the head of the recursive type is removed and the remainder is processed until the type is empty.
2288Each recursion is counted to obtain the number of elements in the type.
2289
2290Note, the compiler statically guarantees that only the correct kind of type is used in the \lstinline[language=OCaml]{match} statement.
2291However, 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.
2292
2293In summary, an OCaml variant is a singleton value rather than a set of possibly ordered values, and hence, has no notion of enumerabilty.
2294Therefore it is not an enumeration, except for the simple opaque (nullary) case.
2295
2296\begin{comment}
2297Date: Wed, 13 Mar 2024 10:52:34 -0400
2298Subject: Re: OCaml
2299To: "Peter A. Buhr" <pabuhr@uwaterloo.ca>
2300From: Gregor Richards <gregor.richards@uwaterloo.ca>
2301
2302On 3/12/24 18:34, Peter A. Buhr wrote:
2303> Gregor, attached is a section Jiada wrote on OCaml (1-page).
2304> Does it reflect our discussion about functional languages and enumerations?
2305
2306Yeah, I think so. The most important part, i.e., that once they're
2307parameterized they're not really enumerations at all, is covered clearly
2308enough.
2309
2310A couple quibbles:
2311
2312<<a list of untyped tags>>
2313
2314This is true, but leaking implementation details. These are nullary datatype
2315constructors. Indeed, you later talk about "tagged variants", which are really
2316just parameterized variants, using the term "tag" differently, confusing the
2317term "tag" further.
2318
2319<<Because week is a summation of values Mon to Sun, it is a sum type in
2320turns of the functional-programming paradigm>>
2321
2322It is a *union* of values and is a *union* type.
2323
2324With valediction,
2325  - Gregor Richards
2326
2327
2328Date: Thu, 14 Mar 2024 21:45:52 -0400
2329Subject: Re: OCaml "enums" do come with ordering
2330To: "Peter A. Buhr" <pabuhr@uwaterloo.ca>
2331From: Gregor Richards <gregor.richards@uwaterloo.ca>
2332
2333On 3/14/24 21:30, Peter A. Buhr wrote:
2334> I've marked 3 places with your name to shows places with enum ordering.
2335>
2336> type week = Mon | Tue | Wed | Thu | Fri | Sat | Sun
2337> let day : week = Mon
2338> let take_class( d : week ) =
2339>       if d <= Fri then                                (* Gregor *)
2340>               Printf.printf "week\n"
2341>       else if d >= Sat then                   (* Gregor *)
2342>               Printf.printf "weekend\n";
2343>       match d with
2344>               Mon | Wed -> Printf.printf "CS442\n" |
2345>               Tue | Thu -> Printf.printf "CS343\n" |
2346>               Fri -> Printf.printf "Tutorial\n" |
2347>               _ -> Printf.printf "Take a break\n"
2348>
2349> let _ = take_class( Mon ); take_class( Sat );
2350>
2351> type colour = Red | Green of string | Blue of int * float
2352> let c = Red
2353> let _ = match c with Red -> Printf.printf "Red, "
2354> let c = Green( "abc" )
2355> let _ = match c with Green g -> Printf.printf "%s, " g
2356> let c = Blue( 1, 1.5 )
2357> let _ = match c with Blue( i, f ) -> Printf.printf "%d %g\n" i f
2358>
2359> let check_colour(c: colour): string =
2360>       if c < Green( "xyz" ) then              (* Gregor *)
2361>               Printf.printf "green\n";
2362>       match c with
2363>               Red -> "Red" |
2364>               Green g -> g |
2365>               Blue(i, f) -> string_of_int i ^ string_of_float f
2366> let _ = check_colour( Red ); check_colour( Green( "xyz" ) );
2367>
2368> type stringList = Empty | Pair of string * stringList
2369> let rec len_of_string_list(l: stringList): int =
2370>       match l with
2371>               Empty -> 0 |
2372>               Pair(_ , r) -> 1 + len_of_string_list r
2373>
2374> let _ = for i = 1 to 10 do
2375>       Printf.printf "%d, " i
2376> done
2377>
2378> (* Local Variables: *)
2379> (* tab-width: 4 *)
2380> (* compile-command: "ocaml test.ml" *)
2381> (* End: *)
2382
2383My functional-language familiarity is far more with Haskell than OCaml.  I
2384mostly view OCaml through a lens of "it's Haskell but with cheating".  Haskell
2385"enums" (ADTs) aren't ordered unless you specifically and manually put them in
2386the Ord typeclass by defining the comparators.  Apparently, OCaml has some
2387other rule, which I would guess is something like "sort by tag then by order of
2388parameter". Having a default behavior for comparators is *bizarre*; my guess
2389would be that it gained this behavior in its flirtation with object
2390orientation, but that's just a guess (and irrelevant).
2391
2392This gives a total order, but not enumerability (which would still be
2393effectively impossible or even meaningless since enums are just a special case
2394of ADTs).
2395
2396With valediction,
2397  - Gregor Richards
2398
2399Date: Wed, 20 Mar 2024 18:16:44 -0400
2400Subject: Re:
2401To: "Peter A. Buhr" <pabuhr@uwaterloo.ca>
2402From: Gregor Richards <gregor.richards@uwaterloo.ca>
2403
2404
2405On 3/20/24 17:26, Peter A. Buhr wrote:
2406> Gregor, everyone at this end would like a definition of "enumerability". Can
2407> you formulate one?
2408
2409According to the OED (emphasis added to the meaning I'm after):
2410
2411enumerate (verb, transitive). To count, ascertain the number of; **more
2412usually, to mention (a number of things or persons) separately, as if for the
2413purpose of counting**; to specify as in a list or catalogue.
2414
2415With C enums, if you know the lowest and highest value, you can simply loop
2416over them in a for loop (this is, of course, why so many enums come with an
2417ENUM_WHATEVER_LAST value). But, I would be hesitant to use the word "loop" to
2418describe enumerability, since in functional languages, you would recurse for
2419such a purpose.
2420
2421In Haskell, in order to do something with every member of an "enumeration", you
2422would have to explicitly list them all. The type system will help a bit since
2423it knows if you haven't listed them all, but you would have to statically have
2424every element in the enumeration.  If somebody added new elements to the
2425enumeration later, your code to enumerate over them would no longer work
2426correctly, because you can't simply say "for each member of this enumeration do
2427X". In Haskell that's because there aren't actually enumerations; what they use
2428as enumerations are a degenerate form of algebraic datatypes, and ADTs are
2429certainly not enumerable. In OCaml, you've demonstrated that they impose
2430comparability, but I would still assume that you can't make a loop over every
2431member of an enumeration. (But, who knows!)
2432
2433Since that's literally what "enumerate" means, it seems like a rather important
2434property for enumerations to have ;)
2435
2436With valediction,
2437  - Gregor Richards
2438
2439
2440From: Andrew James Beach <ajbeach@uwaterloo.ca>
2441To: Gregor Richards <gregor.richards@uwaterloo.ca>, Peter Buhr <pabuhr@uwaterloo.ca>
2442CC: Michael Leslie Brooks <mlbrooks@uwaterloo.ca>, Fangren Yu <f37yu@uwaterloo.ca>,
2443    Jiada Liang <j82liang@uwaterloo.ca>
2444Subject: Re: Re:
2445Date: Thu, 21 Mar 2024 14:26:36 +0000
2446
2447Does this mean that not all enum declarations in C create enumerations? If you
2448declare an enumeration like:
2449
2450enum Example {
2451    Label,
2452    Name = 10,
2453    Tag = 3,
2454};
2455
2456I don't think there is any way to enumerate (iterate, loop, recurse) over these
2457values without listing all of them.
2458
2459
2460Date: Thu, 21 Mar 2024 10:31:49 -0400
2461Subject: Re:
2462To: Andrew James Beach <ajbeach@uwaterloo.ca>, Peter Buhr <pabuhr@uwaterloo.ca>
2463CC: Michael Leslie Brooks <mlbrooks@uwaterloo.ca>, Fangren Yu <f37yu@uwaterloo.ca>,
2464    Jiada Liang <j82liang@uwaterloo.ca>
2465From: Gregor Richards <gregor.richards@uwaterloo.ca>
2466
2467I consider this conclusion reasonable. C enums can be nothing more than const
2468ints, and if used in that way, I personally wouldn't consider them as
2469enumerations in any meaningful sense, particularly since the type checker
2470essentially does nothing for you there. Then they're a way of writing consts
2471repeatedly with some textual indicator that these definitions are related; more
2472namespace, less enum.
2473
2474When somebody writes bitfield members as an enum, is that *really* an
2475enumeration, or just a use of the syntax for enums to keep related definitions
2476together?
2477
2478With valediction,
2479  - Gregor Richards
2480
2481
2482Date: Tue, 16 Apr 2024 11:04:51 -0400
2483Subject: Re: C unnamed enumeration
2484To: "Peter A. Buhr" <pabuhr@uwaterloo.ca>
2485CC: <ajbeach@uwaterloo.ca>, <j82liang@uwaterloo.ca>, <mlbrooks@uwaterloo.ca>,
2486        <f37yu@uwaterloo.ca>
2487From: Gregor Richards <gregor.richards@uwaterloo.ca>
2488
2489On 4/16/24 09:55, Peter A. Buhr wrote:
2490> So what is a variant? Is it a set of tag names, which might be a union or is it
2491> a union, which might have tag names?
2492
2493Your tagless variant bears no resemblance to variants in any functional
2494programming language. A variant is a tag AND a union. You might not need to put
2495anything in the union, in which case it's a pointless union, but the named tag
2496is absolutely mandatory. That's the thing that varies.
2497
2498I was unaware of std::variant. As far as functional languages are concerned,
2499std::variant IS NOT A VARIANT. Perhaps it would be best to use the term ADT for
2500the functional language concept, because that term has no other meanings.
2501
2502An ADT cannot not have a named tag. That's meaningless. The tag is the data
2503constructor, which is the thing you actually define when you define an ADT. It
2504is strictly the union that's optional.
2505
2506With valediction,
2507  - Gregor Richards
2508\end{comment}
2509
2510
2511\section{Comparison}
2512
2513\VRef[Table]{t:FeatureLanguageComparison} shows a comparison of enumeration features and programming languages.
2514The features are high level and may not capture nuances within a particular language
2515The @const@ feature is simple macros substitution and not a typed enumeration.
2516
2517\begin{table}
2518\caption{Enumeration Feature / Language Comparison}
2519\label{t:FeatureLanguageComparison}
2520\small
2521\setlength{\tabcolsep}{3pt}
2522\newcommand{\CM}{\checkmark}
2523\begin{tabular}{r|c|c|c|c|c|c|c|c|c|c|c|c|c}
2524                                &Pascal & Ada   &\Csharp& OCaml & Java  &Modula-3&Golang& Rust  & Swift & Python& C             & \CC   & \CFA  \\
2525\hline
2526@const@                 & \CM   &               &               &               &               &               & \CM   &               &               &               &               & \CM   &               \\
2527\hline
2528\hline
2529opaque                  &               &               &               &               &               &               &               &               &               &               &               &               & \CM   \\
2530\hline
2531typed                   &               &               &               &               &               &               &               &               &               &               & @int@ & integral      & @T@   \\
2532\hline
2533safe                    &               &               &               &               &               &               &               &               &               &               &               & \CM   & \CM   \\
2534\hline
2535ordered                 &               &               &               &               &               &               &               &               &               &               & \CM   & \CM   & \CM   \\
2536\hline
2537dup. values             &               &               &               &               &               &               &               &               &               & alias & \CM   & \CM   & \CM   \\
2538\hline
2539setable                 &               &               &               &               &               &               &               &               &               &               & \CM   & \CM   & \CM   \\
2540\hline
2541auto-init               &               &               &               &               &               &               &               &               &               &               & \CM   & \CM   & \CM   \\
2542\hline
2543(un)scoped              &               &               &               &               &               &               &               &               &               &               & U             & U/S   & U/S   \\
2544\hline
2545overload                &               & \CM   &               &               &               &               &               &               &               &               &               & \CM   & \CM   \\
2546\hline
2547switch                  &               &               &               &               &               &               &               &               &               &               & \CM   & \CM   & \CM   \\
2548\hline
2549loop                    &               &               &               &               &               &               &               &               &               &               &               &               & \CM   \\
2550\hline
2551array/subscript &               &               &               &               &               &               &               &               &               &               & \CM   &               & \CM   \\
2552\hline
2553subtype                 &               &               &               &               &               &               &               &               &               &               &               &               & \CM   \\
2554\hline
2555inheritance             &               &               &               &               &               &               &               &               &               &               &               &               & \CM   \\
2556\end{tabular}
2557\end{table}
Note: See TracBrowser for help on using the repository browser.