[956299b] | 1 | \chapter{Related Work} |
---|
| 2 | \label{s:RelatedWork} |
---|
| 3 | |
---|
[7d9a805b] | 4 | \begin{comment} |
---|
[282061a] | 5 | An algebraic data type (ADT) can be viewed as a recursive sum of product types. |
---|
| 6 | A sum type lists values as members. |
---|
| 7 | A member in a sum type definition is known as a data constructor. |
---|
| 8 | For example, C supports sum types union and enumeration (enum). |
---|
| 9 | An enumeration in C can be viewed as the creation of a list of zero-arity data constructors. |
---|
| 10 | A union instance holds a value of one of its member types. |
---|
| 11 | Defining a union does not generate new constructors. |
---|
| 12 | The definition of member types and their constructors are from the outer lexical scope. |
---|
| 13 | |
---|
[caaf424] | 14 | In general, an \newterm{algebraic data type} (ADT) is a composite type, \ie, a type formed by combining other types. |
---|
| 15 | Three common classes of algebraic types are \newterm{array type}, \ie homogeneous types, \newterm{product type}, \ie heterogeneous tuples and records (structures), and \newterm{sum type}, \ie tagged product-types (unions). |
---|
[9262fe9] | 16 | Enumerated types are a special case of product/sum types with non-mutable fields, \ie initialized (constructed) once at the type's declaration, possible restricted to compile-time initialization. |
---|
| 17 | Values of algebraic types are access by subscripting, field qualification, or type (pattern) matching. |
---|
[7d9a805b] | 18 | \end{comment} |
---|
[956299b] | 19 | |
---|
[ec20ab9] | 20 | Enumeration-like features exist in many popular programming languages, both past and present, \eg Pascal~\cite{Pascal}, Ada~\cite{Ada}, \Csharp~\cite{Csharp}, OCaml~\cite{OCaml} \CC, Go~\cite{Go}, Haskell~\cite{Haskell}, Java~\cite{Java}, Rust~\cite{Rust}, Swift~\cite{Swift}, Python~\cite{Python}. |
---|
[9262fe9] | 21 | Among theses languages, there are a large set of overlapping features, but each language has its own unique extensions and restrictions. |
---|
[f936e23] | 22 | |
---|
[38f5006] | 23 | \section{Pascal} |
---|
[7d9a805b] | 24 | \label{s:Pascal} |
---|
[f936e23] | 25 | |
---|
[ec20ab9] | 26 | Classic Pascal introduced the \lstinline[language=Pascal]{const} aliasing declaration binding a name to a constant literal/expression. |
---|
[38f5006] | 27 | \begin{pascal} |
---|
| 28 | const one = 0 + 1; Vowels = set of (A,E,I,O,U); NULL = NIL; |
---|
| 29 | PI = 3.14159; Plus = '+'; Fred = 'Fred'; |
---|
| 30 | \end{pascal} |
---|
[f632117] | 31 | As stated, this mechanism is not an enumeration because there is no specific type (pseudo enumeration). |
---|
[1d5e5601] | 32 | Hence, there is no notion of a (possibly ordered) set, modulo the \lstinline[language=pascal]{set of} type. |
---|
| 33 | The type of each constant name (enumerator) is inferred from the constant-expression type. |
---|
[38f5006] | 34 | |
---|
[022bce0] | 35 | Free Pascal~\cite[\S~3.1.1]{FreePascal} is a modern, object-oriented version of classic Pascal, with a C-style enumeration type. |
---|
| 36 | Enumerators must be assigned in ascending numerical order with a constant expression and the range can be non-consecutive. |
---|
[f936e23] | 37 | \begin{pascal} |
---|
[956299b] | 38 | Type EnumType = ( one, two, three, forty @= 40@, fortyone ); |
---|
[f936e23] | 39 | \end{pascal} |
---|
[956299b] | 40 | Pseudo-functions @Pred@ and @Succ@ can only be used if the range is consecutive. |
---|
[022bce0] | 41 | The underlying type is an implementation-defined integral-type large enough to hold all enumerated values; it does not have to be the smallest possible type. |
---|
| 42 | The integral size can be explicitly specified using compiler directive @$PACKENUM@~$N$, where $N$ is the number of bytes, \eg: |
---|
[f936e23] | 43 | \begin{pascal} |
---|
[956299b] | 44 | Type @{$\color{red}\$$PACKENUM 1}@ SmallEnum = ( one, two, three ); |
---|
[c033405] | 45 | @{$\color{red}\$$PACKENUM 4}@ LargeEnum = ( BigOne, BigTwo, BigThree ); |
---|
[956299b] | 46 | Var S : SmallEnum; { 1 byte } |
---|
| 47 | L : LargeEnum; { 4 bytes} |
---|
[f936e23] | 48 | \end{pascal} |
---|
[956299b] | 49 | |
---|
| 50 | |
---|
| 51 | \section{Ada} |
---|
[f936e23] | 52 | |
---|
[caaf424] | 53 | An Ada enumeration type is a set of ordered unscoped identifiers (enumerators) bound to \emph{unique} \newterm{literals}.\footnote{% |
---|
[4da9142] | 54 | Ada is \emph{case-insensitive} so identifiers may appear in multiple forms and still be the same, \eg \lstinline{Mon}, \lstinline{moN}, and \lstinline{MON} (a questionable design decision).} |
---|
[f936e23] | 55 | \begin{ada} |
---|
[4da9142] | 56 | type Week is ( Mon, Tue, Wed, Thu, Fri, Sat, Sun ); -- literals (enumerators) |
---|
[f936e23] | 57 | \end{ada} |
---|
[1d5e5601] | 58 | Object initialization and assignment are restricted to the enumerators of this type. |
---|
[4da9142] | 59 | While Ada enumerators are unscoped, like C, Ada enumerators are overloadable. |
---|
[f936e23] | 60 | \begin{ada} |
---|
[4da9142] | 61 | type RGB is ( @Red@, @Green@, Blue ); |
---|
[38f5006] | 62 | type Traffic_Light is ( @Red@, Yellow, @Green@ ); |
---|
[f936e23] | 63 | \end{ada} |
---|
[ec20ab9] | 64 | Like \CFA, Ada uses a type-resolution algorithm including the left-hand side of assignmente to disambiguate among overloaded identifiers. |
---|
[282061a] | 65 | \VRef[Figure]{f:AdaEnumeration} shows how ambiguity is handled using a cast, \ie \lstinline[language=ada]{RGB'(Red)}. |
---|
[956299b] | 66 | |
---|
[38f5006] | 67 | \begin{figure} |
---|
[f936e23] | 68 | \begin{ada} |
---|
[38f5006] | 69 | with Ada.Text_IO; use Ada.Text_IO; |
---|
| 70 | procedure test is |
---|
[7d9a805b] | 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; |
---|
[38f5006] | 79 | begin |
---|
[7d9a805b] | 80 | @Red@( Blue ); -- RGB |
---|
| 81 | @Red@( Yellow ); -- Traffic_Light |
---|
| 82 | @Red@( @RGB'(Red)@ ); -- ambiguous without cast |
---|
[38f5006] | 83 | end test; |
---|
[f936e23] | 84 | \end{ada} |
---|
[38f5006] | 85 | \caption{Ada Enumeration Overload Resolution} |
---|
| 86 | \label{f:AdaEnumeration} |
---|
| 87 | \end{figure} |
---|
| 88 | |
---|
[4da9142] | 89 | Enumerators without initialization are auto-initialized from left to right, starting at zero, incrementing by 1. |
---|
| 90 | Enumerators with initialization must set \emph{all} enumerators in \emph{ascending} order, \ie there is no auto-initialization. |
---|
| 91 | \begin{ada} |
---|
| 92 | type Week is ( Mon, Tue, Wed, Thu, Fri, Sat, Sun ); |
---|
| 93 | for Week use ( Mon => 0, Tue => 1, Wed => 2, Thu => @10@, Fri => 11, Sat => 14, Sun => 15 ); |
---|
| 94 | \end{ada} |
---|
| 95 | The enumeration operators are the equality and relational operators, @=@, @/=@, @<@, @<=@, @=@, @/=@, @>=@, @>@, where the ordering relationship is given implicitly by the sequence of acsending enumerators. |
---|
| 96 | |
---|
| 97 | Ada provides an alias mechanism, \lstinline[language=ada]{renames}, for aliasing types, which is useful to shorten package identifiers. |
---|
[f936e23] | 98 | \begin{ada} |
---|
[ec20ab9] | 99 | @OtherRed@ : RGB renames Red; |
---|
[f936e23] | 100 | \end{ada} |
---|
[022bce0] | 101 | which suggests a possible \CFA extension to @typedef@. |
---|
[7bb516f] | 102 | \begin{cfa} |
---|
| 103 | typedef RGB.Red OtherRed; |
---|
| 104 | \end{cfa} |
---|
[956299b] | 105 | |
---|
[022bce0] | 106 | There 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@{}} |
---|
[38f5006] | 110 | \begin{ada} |
---|
[022bce0] | 111 | RGB'Pos( Red ) = 0; |
---|
| 112 | RGB'Enum_Rep( Red ) = 10; |
---|
| 113 | RGB'Image( Red ) = "RED"; |
---|
[38f5006] | 114 | \end{ada} |
---|
[022bce0] | 115 | & |
---|
[38f5006] | 116 | \begin{ada} |
---|
[022bce0] | 117 | RGB'Val( 0 ) = Red |
---|
| 118 | RGB'Enum_Val( 10 ) = Red |
---|
| 119 | RGB'Value( "Red" ) = Red |
---|
[38f5006] | 120 | \end{ada} |
---|
[022bce0] | 121 | \end{tabular} |
---|
| 122 | \end{cquote} |
---|
| 123 | These attributes are important for IO. |
---|
| 124 | An enumeration type @T@ also has the following attributes: @T'First@, @T'Last@, @T'Range@, @T'Pred@, @T'Succ@, @T'Min@, and @T'Max@, producing an intuitive result based on the attribute name. |
---|
[956299b] | 125 | |
---|
[022bce0] | 126 | Ada allows the enumerator label to be a character constant. |
---|
[f936e23] | 127 | \begin{ada} |
---|
[022bce0] | 128 | type Operator is ( '+', '-', '*', '/' ); |
---|
[f936e23] | 129 | \end{ada} |
---|
[022bce0] | 130 | which is syntactic sugar for the label and not character literals from the predefined type @Character@. |
---|
[4da9142] | 131 | The purpose is strictly readability using character literals rather than identifiers. |
---|
[f936e23] | 132 | \begin{ada} |
---|
[1d5e5601] | 133 | Op : Operator := '+'; |
---|
| 134 | if Op = '+' or else Op = '-' then ... ; |
---|
| 135 | elsif Op = '*' or else Op = '/' then ... ; end if; |
---|
[f936e23] | 136 | \end{ada} |
---|
[1d5e5601] | 137 | Interestingly, arrays of character enumerators can be treated as strings. |
---|
[022bce0] | 138 | \begin{ada} |
---|
| 139 | Ops : array( 0..3 ) of Operator; |
---|
| 140 | Ops := @"+-*/"@; -- string assignment to array elements |
---|
[f632117] | 141 | Ops := "+-" @&@ "*/"; -- string concatenation and assignment |
---|
[022bce0] | 142 | \end{ada} |
---|
| 143 | Ada's @Character@ type is defined as a character enumeration across all Latin-1 characters. |
---|
[956299b] | 144 | |
---|
[1d5e5601] | 145 | Ada's boolean type is also a special enumeration, which can be used in conditions. |
---|
[f936e23] | 146 | \begin{ada} |
---|
[38f5006] | 147 | type Boolean is (False, True); -- False / True not keywords |
---|
| 148 | @Flag@ : Boolean; |
---|
[022bce0] | 149 | if @Flag@ then ... -- conditional |
---|
[f936e23] | 150 | \end{ada} |
---|
[38f5006] | 151 | Since only types derived from @Boolean@ can be a conditional, @Boolean@ is essentially a builtin type. |
---|
[956299b] | 152 | |
---|
[022bce0] | 153 | Ada provides \emph{consecutive} subtyping of an enumeration using \lstinline[language=ada]{range}. |
---|
[f936e23] | 154 | \begin{ada} |
---|
[022bce0] | 155 | type Week is ( Mon, Tue, Wed, Thu, Fri, Sat, Sun ); |
---|
| 156 | subtype Weekday is Week @range Mon .. Fri@; |
---|
| 157 | subtype Weekend is Week @range Sat .. Sun@; |
---|
| 158 | Day : Week; |
---|
[f936e23] | 159 | \end{ada} |
---|
[38f5006] | 160 | Hence, the ordering of the enumerators is crucial to provide the necessary ranges. |
---|
[956299b] | 161 | |
---|
[ec20ab9] | 162 | An enumeration type can be used in the Ada \lstinline[language=ada]{case} (all enumerators must appear or a @default@) or iterating constructs. |
---|
[022bce0] | 163 | \begin{cquote} |
---|
| 164 | \setlength{\tabcolsep}{15pt} |
---|
| 165 | \begin{tabular}{@{}ll@{}} |
---|
[7bb516f] | 166 | \begin{ada} |
---|
[022bce0] | 167 | case Day is |
---|
| 168 | when @Mon .. Fri@ => ... ; |
---|
| 169 | when @Sat .. Sun@ => ... ; |
---|
| 170 | end case; |
---|
[7bb516f] | 171 | \end{ada} |
---|
[022bce0] | 172 | & |
---|
[f936e23] | 173 | \begin{ada} |
---|
[022bce0] | 174 | case Day is |
---|
| 175 | when @Weekday@ => ... ; -- subtype ranges |
---|
| 176 | when @Weekend@ => ... ; |
---|
[956299b] | 177 | end case; |
---|
[f936e23] | 178 | \end{ada} |
---|
[022bce0] | 179 | \end{tabular} |
---|
| 180 | \end{cquote} |
---|
| 181 | |
---|
| 182 | \begin{cquote} |
---|
| 183 | \setlength{\tabcolsep}{12pt} |
---|
| 184 | \begin{tabular}{@{}lll@{}} |
---|
| 185 | \begin{ada} |
---|
| 186 | for Day in @Mon .. Sun@ loop |
---|
| 187 | ... |
---|
| 188 | end loop; |
---|
| 189 | \end{ada} |
---|
| 190 | & |
---|
| 191 | \begin{ada} |
---|
| 192 | for Day in @Weekday@ loop |
---|
| 193 | ... |
---|
| 194 | end loop; |
---|
| 195 | \end{ada} |
---|
| 196 | & |
---|
[f936e23] | 197 | \begin{ada} |
---|
[022bce0] | 198 | for Day in @Weekend@ loop |
---|
| 199 | ... |
---|
| 200 | end loop; |
---|
[f936e23] | 201 | \end{ada} |
---|
[022bce0] | 202 | \end{tabular} |
---|
| 203 | \end{cquote} |
---|
| 204 | |
---|
| 205 | An enumeration type can be used as an array dimension and subscript. |
---|
[f936e23] | 206 | \begin{ada} |
---|
[022bce0] | 207 | Lunch : array( @Week@ ) of Time; |
---|
| 208 | for Day in Week loop |
---|
| 209 | Lunch( @Day@ ) := ... ; -- set lunch time |
---|
| 210 | end loop; |
---|
[f936e23] | 211 | \end{ada} |
---|
[956299b] | 212 | |
---|
[f936e23] | 213 | |
---|
| 214 | \section{\CC} |
---|
| 215 | \label{s:C++RelatedWork} |
---|
| 216 | |
---|
[f632117] | 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. |
---|
[d69f7114] | 220 | (Note, a \CC @constexpr@ declaration is the same as @const@ with the restriction that the initialization is a compile-time expression.) |
---|
[7d9a805b] | 221 | \begin{c++} |
---|
[f632117] | 222 | const @auto@ one = 0 + 1; $\C{// static initialization}$ |
---|
| 223 | const @auto@ NIL = nullptr; |
---|
| 224 | const @auto@ PI = 3.14159; |
---|
| 225 | const @auto@ Plus = '+'; |
---|
| 226 | const @auto@ Fred = "Fred"; |
---|
| 227 | const @auto@ Mon = 0, Tue = Mon + 1, Wed = Tue + 1, Thu = Wed + 1, Fri = Thu + 1, |
---|
[7d9a805b] | 228 | Sat = Fri + 1, Sun = Sat + 1; |
---|
[f632117] | 229 | void foo() { |
---|
| 230 | const @auto@ r = random(); $\C{// dynamic initialization}$ |
---|
| 231 | int va[r]; $\C{// VLA, auto scope only}$ |
---|
| 232 | } |
---|
[7d9a805b] | 233 | \end{c++} |
---|
| 234 | Statically initialized identifiers may appear in any constant-expression context, \eg @case@. |
---|
[f632117] | 235 | Dynamically initialized identifiers may appear as array dimensions in @g++@, which allows variable-sized arrays. |
---|
| 236 | Interestingly, global \CC @const@ declarations are implicitly marked @static@ (@r@, read-only local, rather than @R@, read-only external) |
---|
[7d9a805b] | 237 | \begin{c++} |
---|
| 238 | $\$$ nm test.o |
---|
| 239 | 0000000000000018 @r@ Mon |
---|
| 240 | \end{c++} |
---|
[f632117] | 241 | whereas C @const@ declarations without @static@ are marked @R@. |
---|
[7d9a805b] | 242 | |
---|
[ec20ab9] | 243 | The following \CC non-backwards compatible changes are made \see{\cite[\S~7.2]{ANSI98:c++}}. |
---|
[38f5006] | 244 | \begin{cquote} |
---|
[f632117] | 245 | Change: \CC objects of enumeration type can only be assigned values of the same enumeration type. |
---|
[f936e23] | 246 | In C, objects of enumeration type can be assigned values of any integral type. \\ |
---|
| 247 | Example: |
---|
| 248 | \begin{c++} |
---|
| 249 | enum color { red, blue, green }; |
---|
[ec20ab9] | 250 | color c = 1; $\C{// valid C, invalid c++}$ |
---|
[f936e23] | 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. |
---|
[38f5006] | 256 | \end{cquote} |
---|
| 257 | |
---|
| 258 | \begin{cquote} |
---|
[f632117] | 259 | Change: In \CC, the type of an enumerator is its enumeration. |
---|
[f936e23] | 260 | In C, the type of an enumerator is @int@. \\ |
---|
| 261 | Example: |
---|
| 262 | \begin{c++} |
---|
| 263 | enum e { A }; |
---|
| 264 | sizeof(A) == sizeof(int) $\C{// in C}$ |
---|
[ec20ab9] | 265 | sizeof(A) == sizeof(e) $\C{// in c++}$ |
---|
[f936e23] | 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. |
---|
| 272 | Taking the size of an enumerator is not a common C coding practice. |
---|
[38f5006] | 273 | \end{cquote} |
---|
[f936e23] | 274 | Hence, the values in a \CC enumeration can only be its enumerators (without a cast). |
---|
| 275 | While the storage size of an enumerator is up to the compiler, there is still an implicit cast to @int@. |
---|
| 276 | \begin{c++} |
---|
| 277 | enum E { A, B, C }; |
---|
| 278 | E e = A; |
---|
[282061a] | 279 | int i = A; i = e; $\C{// implicit casts to int}$ |
---|
[f936e23] | 280 | \end{c++} |
---|
[ec20ab9] | 281 | \CC{11} added a scoped enumeration, \lstinline[language=c++]{enum class} (or \lstinline[language=c++]{enum struct})\footnote{ |
---|
| 282 | The use of keyword \lstinline[language=c++]{class} is resonable because default visibility is \lstinline[language=c++]{private} (scoped). |
---|
| 283 | However, default visibility for \lstinline[language=c++]{struct} is \lstinline[language=c++]{public} (unscoped) making it an odd choice.}, |
---|
| 284 | where the enumerators are accessed using type qualification. |
---|
[f936e23] | 285 | \begin{c++} |
---|
| 286 | enum class E { A, B, C }; |
---|
| 287 | E e = @E::@A; $\C{// qualified enumerator}$ |
---|
[1d5e5601] | 288 | e = B; $\C{// error: B not in scope}$ |
---|
[f936e23] | 289 | \end{c++} |
---|
[022bce0] | 290 | \CC{20} supports explicit unscoping with a \lstinline[language=c++]{using enum} declaration. |
---|
[f936e23] | 291 | \begin{c++} |
---|
| 292 | enum class E { A, B, C }; |
---|
| 293 | @using enum E;@ |
---|
[282061a] | 294 | E e = A; e = B; $\C{// direct access}$ |
---|
[f936e23] | 295 | \end{c++} |
---|
[ec20ab9] | 296 | \CC{11} added the ability to explicitly declare only an underlying \emph{integral} type for \lstinline[language=c++]{enum class}. |
---|
[f936e23] | 297 | \begin{c++} |
---|
| 298 | enum class RGB @: long@ { Red, Green, Blue }; |
---|
| 299 | enum class rgb @: char@ { Red = 'r', Green = 'g', Blue = 'b' }; |
---|
| 300 | enum class srgb @: signed char@ { Red = -1, Green = 0, Blue = 1 }; |
---|
| 301 | \end{c++} |
---|
[1d5e5601] | 302 | There is no implicit conversion from the \lstinline[language=c++]{enum class} type to its declared type. |
---|
[f936e23] | 303 | \begin{c++} |
---|
| 304 | rgb crgb = rgb::Red; |
---|
[1d5e5601] | 305 | char ch = rgb::Red; ch = crgb; $\C{// error}$ |
---|
[f936e23] | 306 | \end{c++} |
---|
[ec20ab9] | 307 | An enumeration can be used in the @if@ and @switch@ statements. |
---|
| 308 | \begin{cquote} |
---|
| 309 | \setlength{\tabcolsep}{15pt} |
---|
| 310 | \begin{tabular}{@{}ll@{}} |
---|
| 311 | \begin{c++} |
---|
| 312 | if ( @day@ <= Fri ) |
---|
| 313 | cout << "weekday" << endl; |
---|
| 314 | |
---|
| 315 | |
---|
| 316 | |
---|
| 317 | |
---|
| 318 | \end{c++} |
---|
| 319 | & |
---|
| 320 | \begin{c++} |
---|
| 321 | switch ( @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} |
---|
| 330 | However, there is no mechanism to iterate through an enumeration without an unsafe cast and it does not understand the enumerator values. |
---|
| 331 | \begin{c++} |
---|
| 332 | enum Week { Mon, Tue, Wed, Thu = 10, Fri, Sat, Sun }; |
---|
| 333 | for ( Week d = Mon; d <= Sun; d = @(Week)(d + 1)@ ) cout << d << ' '; |
---|
| 334 | 0 1 2 @3 4 5 6 7 8 9@ 10 11 12 13 |
---|
| 335 | \end{c++} |
---|
| 336 | An enumeration type cannot declare an array dimension but an enumerator can be used as a subscript. |
---|
| 337 | There is no mechanism to subtype or inherit from an enumeration. |
---|
[f936e23] | 338 | |
---|
| 339 | |
---|
[7bb516f] | 340 | \section{C\raisebox{-0.7ex}{\LARGE$^\sharp$}\xspace} % latex bug: cannot use \relsize{2} so use \LARGE |
---|
[924534e] | 341 | \label{s:Csharp} |
---|
[7bb516f] | 342 | |
---|
| 343 | % https://www.tutorialsteacher.com/codeeditor?cid=cs-mk8Ojx |
---|
[ec20ab9] | 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 |
---|
[7bb516f] | 346 | |
---|
[ec20ab9] | 347 | \Csharp is a dynamically-typed programming-language with a scoped, integral enumeration similar to \CC \lstinline[language=C++]{enum class}. |
---|
[7bb516f] | 348 | \begin{csharp} |
---|
[ec20ab9] | 349 | enum Week : @long@ { Mon, Tue, Wed, Thu@ = 10@, Fri, Sat, Sun@,@ } // terminating comma |
---|
| 350 | enum RGB { Red, Green, Blue } |
---|
[7bb516f] | 351 | \end{csharp} |
---|
[ec20ab9] | 352 | The default underlying integral type is @int@ (no @char@), with auto-incrementing, implicit/explicit initialization, and terminating comma. |
---|
| 353 | A method cannot be defined in an enumeration type (extension methods are possible). |
---|
| 354 | There is an explicit bidirectional conversion between an enumeration and its integral type, and an implicit conversion to the enumerator label in display contexts. |
---|
[7bb516f] | 355 | \begin{csharp} |
---|
[ec20ab9] | 356 | int iday = (int)Week.Fri; $\C{// day == 11}$ |
---|
| 357 | Week day = @(Week)@42; $\C{// day == 42, unsafe}$ |
---|
| 358 | string mon = Week.Mon.ToString(); $\C{// mon == "Mon"}$ |
---|
| 359 | RGB rgb = RGB.Red; $\C{// rgb == "Red"}$ |
---|
| 360 | day = @(Week)@rgb; $\C{// day == "Mon", unsafe}$ |
---|
| 361 | Console.WriteLine( Week.Fri ); $\C{// print label Fri}$ |
---|
[7bb516f] | 362 | \end{csharp} |
---|
[ec20ab9] | 363 | The majority of the integral operators (relational and arithmetic) work with enumerations, except @*@ and @/@. |
---|
[7bb516f] | 364 | \begin{csharp} |
---|
[ec20ab9] | 365 | day = day++ - 5; $\C{// unsafe}$ |
---|
| 366 | day = day & day; |
---|
[7bb516f] | 367 | \end{csharp} |
---|
| 368 | |
---|
[ec20ab9] | 369 | An enumeration can be used in the @if@ and @switch@ statements. |
---|
| 370 | \begin{cquote} |
---|
| 371 | \setlength{\tabcolsep}{15pt} |
---|
| 372 | \begin{tabular}{@{}ll@{}} |
---|
[7bb516f] | 373 | \begin{csharp} |
---|
[ec20ab9] | 374 | if ( @day@ <= Week.Fri ) |
---|
| 375 | Console.WriteLine( "weekday" ); |
---|
[7bb516f] | 376 | |
---|
[924534e] | 377 | |
---|
| 378 | |
---|
| 379 | |
---|
| 380 | |
---|
| 381 | \end{csharp} |
---|
| 382 | & |
---|
[7bb516f] | 383 | \begin{csharp} |
---|
[ec20ab9] | 384 | switch ( @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; |
---|
[7bb516f] | 390 | } |
---|
| 391 | \end{csharp} |
---|
[924534e] | 392 | \end{tabular} |
---|
[ec20ab9] | 393 | \end{cquote} |
---|
| 394 | However, 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} |
---|
| 396 | for ( Week d = Mon; d <= Sun; @d += 1@ ) { |
---|
| 397 | Console.Write( d + " " ); |
---|
| 398 | } |
---|
| 399 | Mon Tue Wed @3 4 5 6 7 8 9@ Thu Fri Sat Sun |
---|
| 400 | \end{csharp} |
---|
| 401 | The @Enum.GetValues@ pseudo-method retrieves an array of the enumeration constants for looping over an enumeration type or variable (expensive operation). |
---|
| 402 | \begin{csharp} |
---|
| 403 | foreach ( Week d in @Enum.GetValues@( typeof(Week) ) ) { |
---|
| 404 | Console.WriteLine( d + " " + (int)d + " " ); // label, position |
---|
| 405 | } |
---|
| 406 | Mon 0, Tue 1, Wed 2, Thu 10, Fri 11, Sat 12, Sun 13, |
---|
| 407 | \end{csharp} |
---|
[c033405] | 408 | Hence, enumerating is not supplied directly by the enumeration, but indirectly through another enumerable type, array. |
---|
[ec20ab9] | 409 | |
---|
| 410 | An enumeration type cannot declare an array dimension but an enumerator can be used as a subscript. |
---|
| 411 | There is no mechanism to subtype or inherit from an enumeration. |
---|
| 412 | |
---|
| 413 | The @Flags@ attribute creates a bit-flags enumeration, making bitwise operators @&@, @|@, @~@ (complement), @^@ (xor) sensible. |
---|
| 414 | \begin{csharp} |
---|
| 415 | @[Flags]@ public enum Week { |
---|
| 416 | None = 0x0, Mon = 0x1, Tue = 0x2, Wed = 0x4, |
---|
| 417 | Thu = 0x8, Fri = 0x10, Sat = 0x20, Sun = 0x40, |
---|
| 418 | Weekdays = @Mon | Tue | Wed | Thu | Fri@ $\C{// Weekdays == 0x1f}$ |
---|
| 419 | Weekend = @Sat | Sun@, $\C{// Weekend == 0x60}$ |
---|
| 420 | } |
---|
| 421 | Week meetings = @Week.Mon | Week.Wed@; $\C{// 0x5}$ |
---|
| 422 | \end{csharp} |
---|
[7bb516f] | 423 | |
---|
| 424 | |
---|
[38f5006] | 425 | \section{Golang} |
---|
[41c4b5e] | 426 | \label{s:Golang} |
---|
[f936e23] | 427 | |
---|
[ec20ab9] | 428 | Golang has a no enumeration. |
---|
| 429 | It has @const@ aliasing declarations, similar to \CC \see{\VRef{s:C++RelatedWork}}, for basic types with type inferencing and static initialization (constant expression). |
---|
[f936e23] | 430 | \begin{Go} |
---|
[ec20ab9] | 431 | const R @int@ = 0; const G @uint@ = 1; const B = 2; $\C{// explicit typing and type inferencing}$ |
---|
| 432 | const Fred = "Fred"; const Mary = "Mary"; const Jane = "Jane"; |
---|
| 433 | const S = 0; const T = 0; |
---|
| 434 | const USA = "USA"; const U = "USA"; |
---|
| 435 | const V = 3.1; const W = 3.1; |
---|
| 436 | \end{Go} |
---|
| 437 | Since these declarations are unmutable variables, they are unscoped and Golang has no overloading. |
---|
| 438 | |
---|
| 439 | Golang provides an enumeration-like feature to group together @const@ declaration into a block and introduces a form of auto-initialization. |
---|
| 440 | \begin{Go} |
---|
| 441 | const ( R = 0; G; B ) $\C{// implicit initialization: 0 0 0}$ |
---|
| 442 | const ( Fred = "Fred"; Mary = "Mary"; Jane = "Jane" ) $\C{// explicit initialization: Fred Mary Jane}$ |
---|
[7bb516f] | 443 | const ( S = 0; T; USA = "USA"; U; V = 3.1; W ) $\C{// type change, implicit/explicit: 0 0 USA USA 3.1 3.1}$ |
---|
[38f5006] | 444 | \end{Go} |
---|
[ec20ab9] | 445 | The first identifier \emph{must} be explicitly initialized; |
---|
| 446 | subsequent identifiers can be implicitly or explicitly initialized. |
---|
| 447 | Implicit initialization is the \emph{previous} (predecessor) identifier value. |
---|
[f936e23] | 448 | |
---|
[ec20ab9] | 449 | Each @const@ declaration provides an implicit integer counter starting at zero, called \lstinline[language=Go]{iota}. |
---|
| 450 | Using \lstinline[language=Go]{iota} outside of a @const@ block always sets the identifier to zero. |
---|
| 451 | \begin{Go} |
---|
| 452 | const R = iota; $\C{// 0}$ |
---|
| 453 | \end{Go} |
---|
| 454 | Inside 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. |
---|
[38f5006] | 455 | \begin{Go} |
---|
| 456 | const ( R = @iota@; G; B ) $\C{// implicit: 0 1 2}$ |
---|
| 457 | const ( C = @iota + B + 1@; G; Y ) $\C{// implicit: 3 4 5}$ |
---|
[f936e23] | 458 | \end{Go} |
---|
[38f5006] | 459 | An underscore \lstinline[language=golang]{const} identifier advances \lstinline[language=Go]{iota}. |
---|
[f936e23] | 460 | \begin{Go} |
---|
[38f5006] | 461 | const ( O1 = iota + 1; @_@; O3; @_@; O5 ) // 1, 3, 5 |
---|
[f936e23] | 462 | \end{Go} |
---|
[ec20ab9] | 463 | Auto-initialization reverts from \lstinline[language=Go]{iota} to the previous value after an explicit initialization, but auto-incrementing of \lstinline[language=Go]{iota} continues. |
---|
[f936e23] | 464 | \begin{Go} |
---|
[924534e] | 465 | const ( Mon = iota; Tue; Wed; // 0, 1, 2 |
---|
[ec20ab9] | 466 | @Thu = 10@; Fri; Sat; Sun = itoa ) // 10, 10, 10, 6 |
---|
[f936e23] | 467 | \end{Go} |
---|
[ec20ab9] | 468 | Auto-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}. |
---|
[f936e23] | 469 | \begin{Go} |
---|
[ec20ab9] | 470 | const ( V1 = iota; V2; @V3 = 7;@ V4 = @iota@ + 1; V5 ) // 0 1 7 4 5 |
---|
[924534e] | 471 | const ( Mon = iota; Tue; Wed; // 0, 1, 2 |
---|
[ec20ab9] | 472 | @Thu = 10;@ Fri = @iota - Wed + Thu - 1@; Sat; Sun ) // 10, 11, 12, 13 |
---|
[f936e23] | 473 | \end{Go} |
---|
[ec20ab9] | 474 | Here, @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}. |
---|
| 475 | Note, because \lstinline[language=Go]{iota} is incremented for an explicitly initialized identifier or @_@, |
---|
| 476 | at @Fri@ \lstinline[language=Go]{iota} is 4 requiring the minus one to compute the value for @Fri@. |
---|
[7bb516f] | 477 | |
---|
[924534e] | 478 | Basic switch and looping are possible. |
---|
| 479 | \begin{cquote} |
---|
[ec20ab9] | 480 | \setlength{\tabcolsep}{20pt} |
---|
[924534e] | 481 | \begin{tabular}{@{}ll@{}} |
---|
| 482 | \begin{Go} |
---|
[ec20ab9] | 483 | day := Mon; // := $\(\Rightarrow\)$ type inferencing |
---|
| 484 | switch @day@ { |
---|
[924534e] | 485 | case Mon, Tue, Wed, Thu, Fri: |
---|
| 486 | fmt.Println( "weekday" ); |
---|
| 487 | case Sat, Sun: |
---|
| 488 | fmt.Println( "weekend" ); |
---|
[7bb516f] | 489 | } |
---|
[924534e] | 490 | \end{Go} |
---|
| 491 | & |
---|
| 492 | \begin{Go} |
---|
[7bb516f] | 493 | |
---|
[ec20ab9] | 494 | for i := @Mon@; i <= @Sun@; i += 1 { |
---|
[1d5e5601] | 495 | fmt.Println( i ) |
---|
[7bb516f] | 496 | } |
---|
| 497 | |
---|
| 498 | |
---|
| 499 | |
---|
[924534e] | 500 | \end{Go} |
---|
| 501 | \end{tabular} |
---|
| 502 | \end{cquote} |
---|
[1d5e5601] | 503 | However, the loop prints the values from 0 to 13 because there is no actual enumeration. |
---|
[7bb516f] | 504 | |
---|
[ec20ab9] | 505 | A constant variable can be used as an array dimension or a subscript. |
---|
| 506 | \begin{Go} |
---|
| 507 | var ar[@Sun@] int |
---|
| 508 | ar[@Mon@] = 3 |
---|
| 509 | \end{Go} |
---|
| 510 | |
---|
[7bb516f] | 511 | |
---|
[924534e] | 512 | \section{Java} |
---|
[7bb516f] | 513 | |
---|
[ec20ab9] | 514 | Java provides an enumeration using a specialized class. |
---|
| 515 | A basic Java enumeration is an opaque enumeration, where the enumerators are constants. |
---|
[7bb516f] | 516 | \begin{Java} |
---|
[ec20ab9] | 517 | enum Week { |
---|
| 518 | Mon, Tue, Wed, Thu, Fri, Sat, Sun; |
---|
| 519 | } |
---|
| 520 | Week day = Week.Sat; |
---|
[7bb516f] | 521 | \end{Java} |
---|
[ec20ab9] | 522 | The enumerators members are scoped and cannot be made \lstinline[language=java]{public}, hence require qualification. |
---|
| 523 | The value of an enumeration instance is restricted to its enumerators. |
---|
| 524 | |
---|
| 525 | The position (ordinal) and label are accessible but there is no value. |
---|
[7bb516f] | 526 | \begin{Java} |
---|
[ec20ab9] | 527 | System.out.println( day.!ordinal()! + " " + !day! + " " + day.!name()! ); |
---|
| 528 | 5 Sat Sat |
---|
[7bb516f] | 529 | \end{Java} |
---|
[ec20ab9] | 530 | Since @day@ has no value, it prints its label (name). |
---|
| 531 | The member @valueOf@ is the inverse of @name@ converting a string to enumerator. |
---|
[7bb516f] | 532 | \begin{Java} |
---|
[ec20ab9] | 533 | day = Week.valueOf( "Wed" ); |
---|
[7bb516f] | 534 | \end{Java} |
---|
[ec20ab9] | 535 | Extra members can be added to provide specialized operations. |
---|
[7bb516f] | 536 | \begin{Java} |
---|
[ec20ab9] | 537 | public boolean isWeekday() { return !ordinal()! <= Fri.ordinal(); } |
---|
[c033405] | 538 | public boolean isWeekend() { return Sat.ordinal() <= !ordinal()!; } |
---|
[7bb516f] | 539 | \end{Java} |
---|
[ec20ab9] | 540 | Notice the unqualified calls to @ordinal@ in the members implying a \lstinline[language=Java]{this} to some implicit implementation variable, likely an @int@. |
---|
[7bb516f] | 541 | |
---|
[ec20ab9] | 542 | Enumerator values require an enumeration type (any Java type may be used) and implementation member. |
---|
[1d5e5601] | 543 | \begin{Java} |
---|
[ec20ab9] | 544 | enum Week { |
---|
[1d5e5601] | 545 | Mon!(1)!, Tue!(2)!, Wed!(3)!, Thu!(4)!, Fri!(5)!, Sat!(6)!, Sun!(7)!; // must appear first |
---|
[ec20ab9] | 546 | private !long! day; $\C{// enumeration type and implementation member}$ |
---|
| 547 | private Week( !long! d ) { day = d; } $\C{// enumerator initialization}$ |
---|
[1d5e5601] | 548 | }; |
---|
[ec20ab9] | 549 | Week day = Week.Sat; |
---|
[1d5e5601] | 550 | \end{Java} |
---|
| 551 | The position, value, and label are accessible. |
---|
| 552 | \begin{Java} |
---|
[ec20ab9] | 553 | System.out.println( !day.ordinal()! + " " + !day.day! + " " + !day.name()! ); |
---|
| 554 | 5 6 Sat |
---|
[1d5e5601] | 555 | \end{Java} |
---|
[ec20ab9] | 556 | If 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@. |
---|
| 557 | The implementation constructor must be private since it is only used internally to initialize the enumerators. |
---|
| 558 | Initialization occurs at the enumeration-type declaration for each enumerator in the first line. |
---|
[1d5e5601] | 559 | |
---|
[ec20ab9] | 560 | Enumerations can be used in the @if@ and @switch@ statements but only for equality tests. |
---|
| 561 | \begin{cquote} |
---|
| 562 | \setlength{\tabcolsep}{15pt} |
---|
| 563 | \begin{tabular}{@{}ll@{}} |
---|
[7bb516f] | 564 | \begin{Java} |
---|
[ec20ab9] | 565 | if ( !day! == Week.Fri ) |
---|
| 566 | System.out.println( "Fri" ); |
---|
| 567 | |
---|
| 568 | |
---|
| 569 | |
---|
| 570 | |
---|
| 571 | \end{Java} |
---|
| 572 | & |
---|
| 573 | \begin{Java} |
---|
| 574 | switch ( !day! ) { |
---|
[924534e] | 575 | case Mon: case Tue: case Wed: case Thu: case Fri: |
---|
[ec20ab9] | 576 | System.out.println( "weekday" ); break; |
---|
[924534e] | 577 | case Sat: case Sun: |
---|
[ec20ab9] | 578 | System.out.println( "weekend" ); break; |
---|
[7bb516f] | 579 | } |
---|
| 580 | \end{Java} |
---|
[ec20ab9] | 581 | \end{tabular} |
---|
| 582 | \end{cquote} |
---|
| 583 | Notice enumerators in the @switch@ statement do not require qualification. |
---|
| 584 | |
---|
| 585 | There 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}. |
---|
| 586 | Like \Csharp, looping over an enumeration is done using method @values@, which returns an array of enumerator values (expensive operation). |
---|
[7bb516f] | 587 | \begin{Java} |
---|
[ec20ab9] | 588 | for ( Week d : Week.values() ) { |
---|
| 589 | System.out.print( d.ordinal() + d.day + " " + d.name() + ", " ); |
---|
[7bb516f] | 590 | } |
---|
[1d5e5601] | 591 | 0 1 Mon, 1 2 Tue, 2 3 Wed, 3 4 Thu, 4 5 Fri, 5 6 Sat, 6 7 Sun, |
---|
[7bb516f] | 592 | \end{Java} |
---|
[c033405] | 593 | Like \Csharp, enumerating is supplied indirectly through another enumerable type, not via the enumeration. |
---|
[7bb516f] | 594 | |
---|
[ec20ab9] | 595 | An enumeration type cannot declare an array dimension nor can an enumerator be used as a subscript. |
---|
| 596 | Enumeration inheritence is disallowed because an enumeration is \lstinline[language=Java]{final}. |
---|
[f936e23] | 597 | |
---|
[ec20ab9] | 598 | Java provides an @EnumSet@ where the underlying type is an efficient set of bits, one per enumeration \see{\Csharp \lstinline{Flags}, \VRef{s:Csharp}}, providing (logical) operations on groups of enumerators. |
---|
| 599 | There is also a specialized version of @HashMap@ with enumerator keys, which has performance benefits. |
---|
[1d5e5601] | 600 | |
---|
| 601 | |
---|
[f936e23] | 602 | \section{Rust} |
---|
[ec20ab9] | 603 | |
---|
[4da9142] | 604 | % https://doc.rust-lang.org/reference/items/enumerations.html |
---|
[1d5e5601] | 605 | |
---|
[c033405] | 606 | Rust @enum@ provides two largely independent mechanisms from a single language feature: an ADT and an enumeration. |
---|
[ec20ab9] | 607 | When @enum@ is an ADT, pattern matching is used to discriminate among the variant types. |
---|
| 608 | \begin{cquote} |
---|
[c033405] | 609 | \begin{tabular}{@{}l@{\hspace{30pt}}ll@{}} |
---|
[1d5e5601] | 610 | \begin{rust} |
---|
[ec20ab9] | 611 | struct S { |
---|
| 612 | i : isize, j : isize |
---|
| 613 | } |
---|
[c033405] | 614 | let mut s = S{ i : 3, j : 4 }; |
---|
[ec20ab9] | 615 | enum @ADT@ { |
---|
[c033405] | 616 | I( isize ), $\C[1in]{// int}$ |
---|
| 617 | F( f64 ), $\C{// float}$ |
---|
| 618 | S( S ), $\C{// struct}\CRT$ |
---|
[ec20ab9] | 619 | } |
---|
[1d5e5601] | 620 | \end{rust} |
---|
[ec20ab9] | 621 | & |
---|
[1d5e5601] | 622 | \begin{rust} |
---|
[ec20ab9] | 623 | let mut adt : ADT; |
---|
[c033405] | 624 | adt = ADT::I(3); println!( "{:?}", adt ); |
---|
| 625 | adt = ADT::F(3.5); println!( "{:?}", adt ); |
---|
| 626 | adt = ADT::S(s); println!( "{:?}", adt ); |
---|
[ec20ab9] | 627 | @match@ adt { |
---|
[c033405] | 628 | ADT::I( i ) => println!( "{:}", i ), |
---|
| 629 | ADT::F( f ) => println!( "{:}", f ), |
---|
| 630 | ADT::S( s ) => println!( "{:} {:}", s.i, s.j ), |
---|
[ec20ab9] | 631 | } |
---|
[c033405] | 632 | \end{rust} |
---|
| 633 | & |
---|
| 634 | \begin{rust} |
---|
| 635 | I(3) |
---|
| 636 | F(3.5) |
---|
| 637 | S(S { i: 3, j: 4 }) |
---|
| 638 | 3 4 |
---|
| 639 | |
---|
| 640 | |
---|
| 641 | |
---|
| 642 | |
---|
| 643 | |
---|
[1d5e5601] | 644 | \end{rust} |
---|
[ec20ab9] | 645 | \end{tabular} |
---|
| 646 | \end{cquote} |
---|
[c033405] | 647 | Even when the variant types are the unit type, the ADT is still not an enumeration because there is no enumerating \see{\VRef{s:AlgebraicDataType}}. |
---|
[1d5e5601] | 648 | \begin{rust} |
---|
[ec20ab9] | 649 | enum Week { Mon, Tues, Wed, Thu, Fri, Sat, Sun@,@ } // terminating comma |
---|
| 650 | let mut week : Week = Week::Mon; |
---|
| 651 | match week { |
---|
| 652 | Week::Mon => println!( "Mon" ), |
---|
| 653 | ... |
---|
| 654 | Week::Sun => println!( "Sun" ), |
---|
[1d5e5601] | 655 | } |
---|
| 656 | \end{rust} |
---|
| 657 | |
---|
[ec20ab9] | 658 | However, Rust allows direct setting of the ADT constructor, which means it is actually a tag. |
---|
[41fb996] | 659 | \begin{cquote} |
---|
[c033405] | 660 | \setlength{\tabcolsep}{15pt} |
---|
[ec20ab9] | 661 | \begin{tabular}{@{}ll@{}} |
---|
[1d5e5601] | 662 | \begin{rust} |
---|
[ec20ab9] | 663 | enum Week { |
---|
| 664 | Mon, Tues, Wed, // start 0 |
---|
| 665 | Thu @= 10@, Fri, |
---|
| 666 | Sat, Sun, |
---|
| 667 | } |
---|
| 668 | |
---|
| 669 | \end{rust} |
---|
| 670 | & |
---|
| 671 | \begin{rust} |
---|
| 672 | #[repr(u8)] |
---|
| 673 | enum ADT { |
---|
| 674 | I(isize) @= 5@, // ??? |
---|
| 675 | F(f64) @= 10@, |
---|
| 676 | S(S) @= 0@, |
---|
| 677 | } |
---|
[1d5e5601] | 678 | \end{rust} |
---|
[ec20ab9] | 679 | \end{tabular} |
---|
| 680 | \end{cquote} |
---|
| 681 | Through 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}. |
---|
| 682 | When tags represent non-unit types, Rust largely precludes accessing the tag because the semantics become meaningless. |
---|
| 683 | Hence, the two mechanisms are largely disjoint, and ony the enumeration component is discussed. |
---|
| 684 | |
---|
| 685 | In detail, the @enum@ type has an implicit integer tag (discriminant), with a unique value for each variant type. |
---|
| 686 | Direct initialization is by a compile-time expression generating a constant value. |
---|
| 687 | Indirect initialization (without initialization, @Fri@/@Sun@) is auto-initialized: from left to right, starting at zero or the next explicitly initialized constant, incrementing by @1@. |
---|
| 688 | There is an explicit cast from the tag to integer. |
---|
[1d5e5601] | 689 | \begin{rust} |
---|
[ec20ab9] | 690 | let mut mon : isize = Week::Mon as isize; |
---|
[1d5e5601] | 691 | \end{rust} |
---|
[ec20ab9] | 692 | An enumeration can be used in the @if@ and \lstinline[language=rust]{match} (@switch@) statements. |
---|
| 693 | \begin{cquote} |
---|
| 694 | \setlength{\tabcolsep}{8pt} |
---|
| 695 | \begin{tabular}{@{}ll@{}} |
---|
| 696 | \begin{c++} |
---|
| 697 | if @week as isize@ == Week::Mon as isize { |
---|
| 698 | println!( "{:?}", week ); |
---|
| 699 | } |
---|
| 700 | |
---|
| 701 | |
---|
| 702 | \end{c++} |
---|
| 703 | & |
---|
| 704 | \begin{c++} |
---|
| 705 | match @week@ { |
---|
| 706 | Week::Mon | Week:: Tue | Week::Wed | Week::Thu |
---|
| 707 | | Week::Fri => println!( "weekday" ), |
---|
| 708 | Week::Sat | Week:: Sun => println!( "weekend" ), |
---|
| 709 | } |
---|
| 710 | \end{c++} |
---|
| 711 | \end{tabular} |
---|
| 712 | \end{cquote} |
---|
[c033405] | 713 | However, there is no mechanism to iterate through an enumeration without casting to integral and positions versus values is not handled. |
---|
[ec20ab9] | 714 | \begin{c++} |
---|
| 715 | for d in Week::Mon as isize ..= Week::Sun as isize { |
---|
| 716 | print!( "{:?} ", d ); |
---|
| 717 | } |
---|
| 718 | 0 1 2 @3 4 5 6 7 8 9@ 10 11 12 13 |
---|
| 719 | \end{c++} |
---|
| 720 | An enumeration type cannot declare an array dimension nor as a subscript. |
---|
| 721 | There is no mechanism to subtype or inherit from an enumeration. |
---|
[f936e23] | 722 | |
---|
| 723 | |
---|
| 724 | \section{Swift} |
---|
[956299b] | 725 | |
---|
[1d5e5601] | 726 | % https://www.programiz.com/swift/online-compiler |
---|
| 727 | |
---|
[c033405] | 728 | Like Rust, Swift @enum@ provides two largely independent mechanisms from a single language feature: an ADT and an enumeration. |
---|
| 729 | When @enum@ is an ADT, pattern matching is used to discriminate among the variant types. |
---|
| 730 | \begin{cquote} |
---|
| 731 | \setlength{\tabcolsep}{20pt} |
---|
| 732 | \begin{tabular}{@{}l@{\hspace{55pt}}ll@{}} |
---|
[f936e23] | 733 | \begin{swift} |
---|
[c033405] | 734 | struct S { |
---|
| 735 | var i : Int, j : Int |
---|
[956299b] | 736 | } |
---|
[c033405] | 737 | var s = S( i : 3, j : 5 ) |
---|
| 738 | @enum@ ADT { |
---|
| 739 | case I(Int) $\C[1.125in]{// int}$ |
---|
| 740 | case F(Float) $\C{// float}$ |
---|
| 741 | case S(S) $\C{// struct}\CRT$ |
---|
[956299b] | 742 | } |
---|
[f936e23] | 743 | \end{swift} |
---|
[c033405] | 744 | & |
---|
[f936e23] | 745 | \begin{swift} |
---|
[c033405] | 746 | var adt : ADT |
---|
| 747 | adt = .I( 3 ); print( adt ) |
---|
| 748 | adt = .F( 3.5 ); print( adt ) |
---|
| 749 | adt = .S( s ); print( adt ) |
---|
| 750 | @switch@ adt { // pattern matching |
---|
| 751 | case .I(let i): print( i ) |
---|
| 752 | case .F(let f): print( f ) |
---|
| 753 | case .S(let s): print( s.i, s.j ) |
---|
[956299b] | 754 | } |
---|
[f936e23] | 755 | \end{swift} |
---|
[c033405] | 756 | & |
---|
[f936e23] | 757 | \begin{swift} |
---|
[c033405] | 758 | I(3) |
---|
| 759 | F(3.5) |
---|
| 760 | S(S(i: 3, j: 5)) |
---|
| 761 | 3 5 |
---|
[956299b] | 762 | |
---|
| 763 | |
---|
| 764 | |
---|
| 765 | |
---|
| 766 | |
---|
[c033405] | 767 | \end{swift} |
---|
| 768 | \end{tabular} |
---|
| 769 | \end{cquote} |
---|
| 770 | (Note, after an @adt@'s type is know, the enumerator is inferred without qualification, \eg @.I(3)@.) |
---|
[956299b] | 771 | |
---|
[c033405] | 772 | An enumeration is created when \emph{all} the enumerators are unit-type. |
---|
[f936e23] | 773 | \begin{swift} |
---|
[c033405] | 774 | enum Week { |
---|
| 775 | case Mon, Tue, Wed, Thu, Fri, Sat, Sun // unit-type |
---|
| 776 | }; |
---|
| 777 | var week : Week = Week.Mon; |
---|
[f936e23] | 778 | \end{swift} |
---|
[c033405] | 779 | As well, it is possible to type \emph{all} the enumerators with a common type, and set different values for each enumerator; |
---|
| 780 | for integral types, there is auto-incrementing. |
---|
| 781 | \begin{cquote} |
---|
| 782 | \setlength{\tabcolsep}{15pt} |
---|
| 783 | \begin{tabular}{@{}lll@{}} |
---|
[f936e23] | 784 | \begin{swift} |
---|
[c033405] | 785 | enum WeekInt: @Int@ { |
---|
| 786 | case Mon, Tue, Wed, Thu = 10, Fri, |
---|
| 787 | Sat = 4, Sun // auto-incrementing |
---|
| 788 | }; |
---|
[f936e23] | 789 | \end{swift} |
---|
[c033405] | 790 | & |
---|
[f936e23] | 791 | \begin{swift} |
---|
[c033405] | 792 | enum WeekStr: @String@ { |
---|
| 793 | case Mon = "MON", Tue, Wed, Thu, Fri, |
---|
| 794 | Sat = "SAT", Sun |
---|
| 795 | }; |
---|
[f936e23] | 796 | \end{swift} |
---|
[c033405] | 797 | \end{tabular} |
---|
| 798 | \end{cquote} |
---|
| 799 | An enumeration only supports equality comparison between enumerator values, unless it inherits from @Comparable@, adding relational operators @<@, @<=@, @>@, and @>=@. |
---|
[956299b] | 800 | |
---|
[c033405] | 801 | An enumeration can have methods. |
---|
[f936e23] | 802 | \begin{swift} |
---|
[c033405] | 803 | enum Week: Comparable { |
---|
| 804 | case Mon, Tue, Wed, Thu, Fri, Sat, Sun // unit-type |
---|
| 805 | func @isWeekday() -> Bool@ { return self <= .Fri } // method |
---|
| 806 | func @isWeekend() -> Bool@ { return .Sat <= self } // method |
---|
| 807 | }; |
---|
[f936e23] | 808 | \end{swift} |
---|
[c033405] | 809 | An enumeration can be used in the @if@ and @switch@ statements, where @switch@ must be exhaustive or have a @default@. |
---|
| 810 | \begin{cquote} |
---|
| 811 | \setlength{\tabcolsep}{15pt} |
---|
| 812 | \begin{tabular}{@{}ll@{}} |
---|
[f936e23] | 813 | \begin{swift} |
---|
[c033405] | 814 | if @week <= .Fri@ { |
---|
| 815 | print( "weekday" ); |
---|
[956299b] | 816 | } |
---|
| 817 | |
---|
| 818 | |
---|
[f936e23] | 819 | \end{swift} |
---|
[c033405] | 820 | & |
---|
[f936e23] | 821 | \begin{swift} |
---|
[c033405] | 822 | switch @week@ { |
---|
| 823 | case .Mon: print( "Mon" ) |
---|
| 824 | ... |
---|
| 825 | case .Sun: print( "Sun" ) |
---|
[956299b] | 826 | } |
---|
[f936e23] | 827 | \end{swift} |
---|
[c033405] | 828 | \end{tabular} |
---|
| 829 | \end{cquote} |
---|
[956299b] | 830 | |
---|
[c033405] | 831 | Enumerating is accomplished by inheriting from @CaseIterable@ without any associated values. |
---|
[f936e23] | 832 | \begin{swift} |
---|
[c033405] | 833 | enum Week: Comparable, @CaseIterable@ { |
---|
| 834 | case Mon, Tue, Wed, Thu, Fri, Sat, Sun // unit-type |
---|
| 835 | }; |
---|
| 836 | var weeki : Week = Week.Mon; |
---|
| 837 | if weeki <= .Fri { |
---|
| 838 | print( "weekday" ); |
---|
[956299b] | 839 | } |
---|
[c033405] | 840 | for day in Week@.allCases@ { |
---|
| 841 | print( day, terminator:" " ) |
---|
[956299b] | 842 | } |
---|
[c033405] | 843 | weekday |
---|
| 844 | Mon Tue Wed Thu Fri Sat Sun |
---|
[f936e23] | 845 | \end{swift} |
---|
[c033405] | 846 | The @enum.allCases@ property returns a collection of all the cases for looping over an enumeration type or variable (expensive operation). |
---|
[956299b] | 847 | |
---|
[c033405] | 848 | A typed enumeration is accomplished by inheriting from any Swift type, and accessing the underlying enumerator value is done with attribute @rawValue@. |
---|
| 849 | Type @Int@ has auto-incrementing from previous enumerator; |
---|
| 850 | type @String@ has auto-incrementing of the enumerator label. |
---|
| 851 | \begin{cquote} |
---|
| 852 | \setlength{\tabcolsep}{15pt} |
---|
| 853 | \begin{tabular}{@{}lll@{}} |
---|
[f936e23] | 854 | \begin{swift} |
---|
[c033405] | 855 | enum WeekInt: @Int@, CaseIterable { |
---|
| 856 | case Mon, Tue, Wed, Thu = 10, Fri, |
---|
| 857 | Sat = 4, Sun // auto-incrementing |
---|
| 858 | }; |
---|
| 859 | for day in WeekInt.allCases { |
---|
| 860 | print( day@.rawValue@, terminator:" " ) |
---|
[956299b] | 861 | } |
---|
[c033405] | 862 | 0 1 2 10 11 4 5 |
---|
[f936e23] | 863 | \end{swift} |
---|
[c033405] | 864 | & |
---|
[f936e23] | 865 | \begin{swift} |
---|
[c033405] | 866 | enum WeekStr: @String@, CaseIterable { |
---|
| 867 | case Mon = "MON", Tue, Wed, Thu, Fri, |
---|
| 868 | Sat = "SAT", Sun |
---|
| 869 | }; |
---|
| 870 | for day in WeekStr.allCases { |
---|
| 871 | print( day@.rawValue@, terminator:" " ) |
---|
[956299b] | 872 | } |
---|
[c033405] | 873 | MON Tue Wed Thu Fri SAT Sun |
---|
[f936e23] | 874 | \end{swift} |
---|
[c033405] | 875 | \end{tabular} |
---|
| 876 | \end{cquote} |
---|
| 877 | |
---|
| 878 | There is a bidirectional conversion from typed enumerator to @rawValue@ and vise versa. |
---|
[f936e23] | 879 | \begin{swift} |
---|
[c033405] | 880 | var weekInt : WeekInt = WeekInt.Mon; |
---|
| 881 | if let opt = WeekInt( rawValue: 0 ) { // test optional return value |
---|
| 882 | print( weekInt.rawValue, opt ) // 0 Mon |
---|
| 883 | } else { |
---|
| 884 | print( "invalid weekday lookup" ) |
---|
[956299b] | 885 | } |
---|
[f936e23] | 886 | \end{swift} |
---|
[c033405] | 887 | Conversion from @rawValue@ to enumerator may fail (bad lookup), so the result is an optional value. |
---|
[956299b] | 888 | |
---|
| 889 | |
---|
[4da9142] | 890 | \section{Python 3.13} |
---|
| 891 | % https://docs.python.org/3/howto/enum.html |
---|
[9262fe9] | 892 | |
---|
[c033405] | 893 | Python is a dynamically-typed reflexive programming language with multiple incompatible versions. |
---|
| 894 | The generality of the language makes it is possible to extend existing or build new language features. |
---|
[41c4b5e] | 895 | As a result, discussing Python enumerations is a moving target, because if a features does not exist, it can often be created with varying levels of complexity within the language. |
---|
[c033405] | 896 | Therefore, the following discussion is (mostly) restricted to the core enumeration features in Python 3.13. |
---|
[9262fe9] | 897 | |
---|
[c033405] | 898 | A Python enumeration is not a basic type; |
---|
| 899 | it is a @class@ inheriting from the @Enum@ class. |
---|
| 900 | The @Enum@ class presents a set of scoped enumerators, where each enumerator is a pair object with a \emph{constant} string name and arbitrary value. |
---|
| 901 | Hence, an enumeration instance is a fixed type (enumeration pair), and its value is the type of one of the enumerator pairs. |
---|
[9262fe9] | 902 | |
---|
[c033405] | 903 | The enumerator value fields must be explicitly initialized and be \emph{unique}. |
---|
[9262fe9] | 904 | \begin{python} |
---|
[c033405] | 905 | class Week(!Enum!): Mon = 1; Tue = 2; Wed = 3; Thu = 4; Fri = 5; Sat = 6; Sun = 7 |
---|
[9262fe9] | 906 | \end{python} |
---|
[c033405] | 907 | and/or explicitly auto initialized, \eg: |
---|
[9262fe9] | 908 | \begin{python} |
---|
[c033405] | 909 | class Week(Enum): Mon = 1; Tue = 2; Wed = 3; Thu = 10; Fri = !auto()!; Sat = 4; Sun = !auto()! |
---|
| 910 | Mon : 1 Tue : 2 Wed : 3 Thu : 10 Fri : !11! Sat : 4 Sun : !12! |
---|
[9262fe9] | 911 | \end{python} |
---|
[c033405] | 912 | where @auto@ increments by 1 from the previous @auto@ value \see{Golang \lstinline[language=Go]{iota}, \VRef{s:Golang}}. |
---|
| 913 | @auto@ is controlled by member @_generate_next_value_()@, which can be overridden: |
---|
[9262fe9] | 914 | \begin{python} |
---|
[c033405] | 915 | @staticmethod |
---|
| 916 | def _generate_next_value_( name, start, count, last_values ): |
---|
| 917 | return name |
---|
[9262fe9] | 918 | \end{python} |
---|
| 919 | |
---|
[c033405] | 920 | There is no direct concept of restricting the enumerators in an enumeration \emph{instance} because the dynamic typing changes the type. |
---|
[9262fe9] | 921 | \begin{python} |
---|
[c033405] | 922 | class RGB(Enum): Red = 1; Green = 2; Blue = 3 |
---|
| 923 | day : Week = Week.Tue; $\C{\# type is Week}$ |
---|
| 924 | !day = RGB.Red! $\C{\# type is RGB}$ |
---|
| 925 | !day : Week = RGB.Red! $\C{\# type is RGB}$ |
---|
[9262fe9] | 926 | \end{python} |
---|
[c033405] | 927 | The enumerators are constants and cannot be reassigned. |
---|
| 928 | Hence, while enumerators can be different types, |
---|
[9262fe9] | 929 | \begin{python} |
---|
[c033405] | 930 | class Diff(Enum): Int = 1; Float = 3.5; Str = "ABC" |
---|
[9262fe9] | 931 | \end{python} |
---|
[c033405] | 932 | it is not an ADT because the enumerator names are not constructors. |
---|
[9262fe9] | 933 | |
---|
[c033405] | 934 | An enumerator initialized with the same value is an alias and invisible at the enumeration level, \ie the alias is substituted for its aliasee. |
---|
[9262fe9] | 935 | \begin{python} |
---|
[c033405] | 936 | class WeekD(Enum): Mon = 1; Tue = 2; Wed = 3; Thu = !10!; Fri = !10!; Sat = !10!; Sun = !10! |
---|
[9262fe9] | 937 | \end{python} |
---|
[c033405] | 938 | Here, the enumeration has only 4 enumerators and 3 aliases. |
---|
| 939 | An alias is only visible by dropping down to the @class@ level and asking for class members. |
---|
| 940 | Aliasing is prevented using the @unique@ decorator. |
---|
[9262fe9] | 941 | \begin{python} |
---|
[c033405] | 942 | !@unique! |
---|
| 943 | class DupVal(Enum): One = 1; Two = 2; Three = !3!; Four = !3! |
---|
| 944 | ValueError: duplicate values found in <enum 'DupVal'>: Four -> Three |
---|
[9262fe9] | 945 | \end{python} |
---|
| 946 | |
---|
[c033405] | 947 | \begin{lrbox}{\myboxA} |
---|
[9262fe9] | 948 | \begin{python} |
---|
[c033405] | 949 | def by_position(enum_type, position): |
---|
| 950 | for index, value in enumerate(enum_type): |
---|
| 951 | if position == index: return value |
---|
| 952 | raise Exception("by_position out of range") |
---|
[9262fe9] | 953 | \end{python} |
---|
[c033405] | 954 | \end{lrbox} |
---|
| 955 | There are bidirectional enumeration pseudo-functions for label and value, but there is no concept of access using ordering (position).\footnote{ |
---|
| 956 | There is an $O(N)$ mechanism to access an enumerator's value by position. \newline \usebox\myboxA} |
---|
| 957 | \begin{cquote} |
---|
| 958 | \setlength{\tabcolsep}{15pt} |
---|
| 959 | \begin{tabular}{@{}ll@{}} |
---|
[9262fe9] | 960 | \begin{python} |
---|
[c033405] | 961 | Week.Thu.value == 4; |
---|
| 962 | Week.Thu.name == "Thu"; |
---|
[9262fe9] | 963 | \end{python} |
---|
[c033405] | 964 | & |
---|
[9262fe9] | 965 | \begin{python} |
---|
[c033405] | 966 | Week( 4 ) == Week.Thu |
---|
| 967 | Week["Thu"].value == 4 |
---|
[9262fe9] | 968 | \end{python} |
---|
[c033405] | 969 | \end{tabular} |
---|
| 970 | \end{cquote} |
---|
| 971 | @Enum@ only supports equality comparison between enumerator values. |
---|
| 972 | There are multiple library extensions to @Enum@, \eg @OrderedEnum@ recipe class, adding relational operators @<@, @<=@, @>@, and @>=@. |
---|
[9262fe9] | 973 | |
---|
[c033405] | 974 | An enumeration \lstinline[language=python]{class} can have methods. |
---|
[9262fe9] | 975 | \begin{python} |
---|
[c033405] | 976 | class Week(!OrderedEnum!): |
---|
| 977 | Mon = 1; Tue = 2; Wed = 3; Thu = 4; Fri = 5; Sat = 6; Sun = 7 |
---|
| 978 | def !isWeekday(self)!: # method |
---|
| 979 | return Week(self.value) !<=! Week.Fri |
---|
| 980 | def !isWeekend(self)!: # method |
---|
| 981 | return Week.Sat !<=! Week(self.value) |
---|
[9262fe9] | 982 | \end{python} |
---|
| 983 | |
---|
[c033405] | 984 | An enumeration can be used in the @if@ and @switch@ statements but only for equality tests, unless extended to @OrderedEnum@. |
---|
| 985 | \begin{cquote} |
---|
| 986 | \setlength{\tabcolsep}{12pt} |
---|
| 987 | \begin{tabular}{@{}ll@{}} |
---|
[9262fe9] | 988 | \begin{python} |
---|
[c033405] | 989 | if day <= Week.Fri : |
---|
| 990 | print( "weekday" ); |
---|
[9262fe9] | 991 | |
---|
| 992 | |
---|
| 993 | |
---|
| 994 | \end{python} |
---|
[c033405] | 995 | & |
---|
[9262fe9] | 996 | \begin{python} |
---|
[c033405] | 997 | match day: |
---|
| 998 | case Week.Mon | Week.Tue | Week.Wed | Week.Thu | Week.Fri: |
---|
| 999 | print( "weekday" ); |
---|
| 1000 | case Week.Sat | Week.Sun: |
---|
| 1001 | print( "weekend" ); |
---|
[9262fe9] | 1002 | \end{python} |
---|
[c033405] | 1003 | \end{tabular} |
---|
| 1004 | \end{cquote} |
---|
| 1005 | Looping is performed using the enumeration type or @islice@ from @itertools@ based on position. |
---|
[9262fe9] | 1006 | \begin{python} |
---|
[c033405] | 1007 | for day in !Week!: $\C[2.25in]{\# Mon : 1 Tue : 2 Wed : 3 Thu : 4 Fri : 5 Sat : 6 Sun : 7}$ |
---|
| 1008 | print( day.name, ":", day.value, end=" " ) |
---|
| 1009 | for day in !islice(Week, 0, 5)!: $\C{\# Mon : 1 Tue : 2 Wed : 3 Thu : 4 Fri : 5}$ |
---|
| 1010 | print( day.name, ":", day.value, end=" " ) |
---|
| 1011 | for day in !islice(Week, 5, 7)!: $\C{\# Sat : 6 Sun : 7}$ |
---|
| 1012 | print( day.name, ":", day.value, end=" " ) |
---|
| 1013 | for day in !islice(Week,0, 7, 2)!: $\C{\# Mon : 1 Wed : 3 Fri : 5 Sun : 7}\CRT$ |
---|
| 1014 | print( day.name, ":", day.value, end=" " ) |
---|
[9262fe9] | 1015 | \end{python} |
---|
[c033405] | 1016 | Iterating that includes alias names only (strings) is done using attribute @__members__@. |
---|
[9262fe9] | 1017 | \begin{python} |
---|
[c033405] | 1018 | for day in WeekD.__members__: |
---|
| 1019 | print( day, ":", end=" " ) |
---|
| 1020 | Mon : Tue : Wed : Thu : Fri : Sat : Sun |
---|
[9262fe9] | 1021 | \end{python} |
---|
| 1022 | |
---|
[c033405] | 1023 | Enumeration subclassing is allowed only if the enumeration base-class does not define any members. |
---|
[9262fe9] | 1024 | \begin{python} |
---|
[c033405] | 1025 | class WeekE(OrderedEnum): !pass!; # no members |
---|
| 1026 | class WeekDay(WeekE): Mon = 1; Tue = 2; Wed = 3; Thu = 4; Fri = 5; |
---|
| 1027 | class WeekEnd(WeekE): Sat = 6; Sun = 7 |
---|
[9262fe9] | 1028 | \end{python} |
---|
[c033405] | 1029 | Here, type @WeekE@ is an abstract type because the dynamic typing never uses it. |
---|
| 1030 | \begin{cquote} |
---|
| 1031 | \setlength{\tabcolsep}{25pt} |
---|
| 1032 | \begin{tabular}{@{}ll@{}} |
---|
[9262fe9] | 1033 | \begin{python} |
---|
[c033405] | 1034 | print( type(WeekE) ) |
---|
| 1035 | day : WeekE = WeekDay.Fri # set type |
---|
| 1036 | print( type(day), day ) |
---|
| 1037 | day = WeekEnd.Sat # set type |
---|
| 1038 | print( type(day), day ) |
---|
[9262fe9] | 1039 | \end{python} |
---|
[c033405] | 1040 | & |
---|
[9262fe9] | 1041 | \begin{python} |
---|
[c033405] | 1042 | <$class$ 'enum.EnumType'> |
---|
[9262fe9] | 1043 | |
---|
[c033405] | 1044 | <enum 'WeekDay'> WeekDay.Fri |
---|
[9262fe9] | 1045 | |
---|
[c033405] | 1046 | <enum 'WeekEnd'> WeekEnd.Sat |
---|
[9262fe9] | 1047 | \end{python} |
---|
[c033405] | 1048 | \end{tabular} |
---|
| 1049 | \end{cquote} |
---|
[9262fe9] | 1050 | |
---|
[c033405] | 1051 | There are a number of supplied enumeration base-types: @IntEnum@, @StrEnum@, @IntFalg@, @Flag@, which restrict the values in an enum using multi-inheritance. |
---|
| 1052 | @IntEnum@ is a subclass of @int@ and @Enum@, allowing enumerator comparison to @int@ and other enumerators of this type (like C enumerators). |
---|
| 1053 | @StrEnum@ is the same as @IntEnum@ but a subclass of the string type \lstinline[language=python]{str}. |
---|
| 1054 | @IntFlag@, is a restricted subclass of @int@ where the enumerators can be combined using the bitwise operators (@&@, @|@, @^@, @~@) and the result is an @IntFlag@ member. |
---|
| 1055 | @Flag@ is the same as @IntFlag@ but cannot be combined with, nor compared against, any other @Flag@ enumeration, nor @int@. |
---|
| 1056 | Auto increment for @IntFlag@ and @Flag@ is by powers of 2. |
---|
| 1057 | Enumerators that are a combinations of single bit enumerators are aliases, and hence, invisible. |
---|
| 1058 | The following is an example for @Flag@. |
---|
[9262fe9] | 1059 | \begin{python} |
---|
[c033405] | 1060 | class WeekF(Flag): Mon = 1; Tue = 2; Wed = 4; Thu = !auto()!; Fri = 16; Sat = 32; Sun = 64; \ |
---|
| 1061 | Weekday = Mon | Tue | Wed | Thu | Fri; \ |
---|
| 1062 | Weekend = Sat | Sun |
---|
| 1063 | print( f"0x{repr(WeekF.Weekday.value)} 0x{repr(WeekF.Weekend.value)}" ) |
---|
| 1064 | 0x31 0x96 |
---|
[9262fe9] | 1065 | \end{python} |
---|
[c033405] | 1066 | It is possible to enumerate through a @Flag@ enumerator (no aliases): |
---|
[9262fe9] | 1067 | \begin{python} |
---|
[c033405] | 1068 | for day in WeekF: |
---|
| 1069 | print( f"{day.name}: {day.value}", end=" ") |
---|
| 1070 | Mon: 1 Tue: 2 Wed: 4 Thu: 8 Fri: 16 Sat: 32 Sun: 64 |
---|
[9262fe9] | 1071 | \end{python} |
---|
[c033405] | 1072 | and a combined alias enumerator for @Flag@. |
---|
| 1073 | \begin{cquote} |
---|
| 1074 | \setlength{\tabcolsep}{15pt} |
---|
| 1075 | \begin{tabular}{@{}ll@{}} |
---|
[9262fe9] | 1076 | \begin{python} |
---|
[c033405] | 1077 | weekday = WeekF.Weekday |
---|
| 1078 | for day in weekday: |
---|
| 1079 | print( f"{day.name}:" |
---|
| 1080 | f" {day.value}", end=" " ) |
---|
| 1081 | Mon: 1 Tue: 2 Wed: 4 Thu: 8 Fri: 16 |
---|
[7d9a805b] | 1082 | \end{python} |
---|
[c033405] | 1083 | & |
---|
[9262fe9] | 1084 | \begin{python} |
---|
[c033405] | 1085 | weekend = WeekF.Weekend |
---|
| 1086 | for day in weekend: |
---|
| 1087 | print( f"{day.name}:" |
---|
| 1088 | f" {day.value}", end=" " ) |
---|
| 1089 | Sat: 32 Sun: 64 |
---|
[9262fe9] | 1090 | \end{python} |
---|
[c033405] | 1091 | \end{tabular} |
---|
| 1092 | \end{cquote} |
---|
[9262fe9] | 1093 | |
---|
[956299b] | 1094 | |
---|
[282061a] | 1095 | \section{OCaml} |
---|
| 1096 | |
---|
[7d9a805b] | 1097 | % https://ocaml.org/docs/basic-data-types#enumerated-data-types |
---|
[d734fa1] | 1098 | % https://dev.realworldocaml.org/runtime-memory-layout.html |
---|
[7d9a805b] | 1099 | |
---|
[223b631] | 1100 | OCaml provides a variant (union) type, where multiple heterogeneously-typed objects share the same storage. |
---|
[4da9142] | 1101 | The simplest form of the variant type is a list of nullary datatype constructors, which is like an unscoped, opaque enumeration. |
---|
[d734fa1] | 1102 | |
---|
| 1103 | OCaml provides a variant (union) type, which is an aggregation of heterogeneous types. |
---|
[4da9142] | 1104 | A basic variant is a list of nullary datatype constructors, which is like an unscoped, opaque enumeration. |
---|
[282061a] | 1105 | \begin{ocaml} |
---|
[ec20ab9] | 1106 | type week = Mon | Tue | Wed | Thu | Fri | Sat | Sun |
---|
| 1107 | let day : week @= Mon@ $\C{(* bind *)}$ |
---|
| 1108 | let take_class( d : week ) = |
---|
[56a8eb8] | 1109 | @match@ d with $\C{(* matching *)}$ |
---|
[9a32903] | 1110 | Mon | Wed -> Printf.printf "CS442\n" | |
---|
| 1111 | Tue | Thu -> Printf.printf "CS343\n" | |
---|
| 1112 | Fri -> Printf.printf "Tutorial\n" | |
---|
| 1113 | _ -> Printf.printf "Take a break\n" |
---|
| 1114 | let _ = take_class( Mon ); |
---|
| 1115 | @CS442@ |
---|
[282061a] | 1116 | \end{ocaml} |
---|
[d734fa1] | 1117 | The only operations are binding and pattern matching (equality), where the variant name is logically the implementation tag stored in the union for discriminating the value in the object storage. |
---|
[84886499] | 1118 | After compilation, variant names are mapped to an opague ascending intergral type discriminants, starting from 0. |
---|
[ec20ab9] | 1119 | Here, 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@. |
---|
| 1120 | The ``@_@'' 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. |
---|
[caaf424] | 1121 | Since the variant has no type, it has a \newterm{0-arity constructor}, \ie no parameters. |
---|
[ec20ab9] | 1122 | Because @week@ is a union of values @Mon@ to @Sun@, it is a \newterm{union type} in turns of the functional-programming paradigm. |
---|
[9398177] | 1123 | |
---|
[6337916] | 1124 | Each variant can have an associated heterogeneous type, with an n-ary constructor for creating a corresponding value. |
---|
[282061a] | 1125 | \begin{ocaml} |
---|
[56a8eb8] | 1126 | type colour = Red | Green of @string@ | Blue of @int * float@ |
---|
[282061a] | 1127 | \end{ocaml} |
---|
[4da9142] | 1128 | A variant with parameter is stored in a memory block, prefixed by an int tag and has its parameters stores as words in the block. |
---|
[56a8eb8] | 1129 | @colour@ is a summation of a nullary type, a unary product type of @string@, and a cross product of @int@ and @float@. |
---|
| 1130 | (Mathematically, a @Blue@ value is a Cartesian product of the types @int@ type and @float@.) |
---|
[9a32903] | 1131 | Hence, a variant type creates a sum of product of different types. |
---|
[282061a] | 1132 | \begin{ocaml} |
---|
[56a8eb8] | 1133 | let c = Red $\C{(* 0-ary constructor, set tag *)}$ |
---|
[9a32903] | 1134 | let _ = match c with Red -> Printf.printf "Red, " |
---|
[56a8eb8] | 1135 | let c = Green( "abc" ) $\C{(* 1-ary constructor, set tag *)}$ |
---|
[9a32903] | 1136 | let _ = match c with Green g -> Printf.printf "%s, " g |
---|
[56a8eb8] | 1137 | let c = Blue( 1, 1.5 ) $\C{(* 2-ary constructor, set tag *)}$ |
---|
[9a32903] | 1138 | let _ = match c with Blue( i, f ) -> Printf.printf "%d %g\n" i f |
---|
| 1139 | @Red, abc, 1 1.5@ |
---|
[282061a] | 1140 | \end{ocaml} |
---|
[9398177] | 1141 | |
---|
[423c0cd] | 1142 | A variant type can have a recursive definition. |
---|
[282061a] | 1143 | \begin{ocaml} |
---|
[56a8eb8] | 1144 | type @stringList@ = Empty | Pair of string * @stringList@ |
---|
[282061a] | 1145 | \end{ocaml} |
---|
[caaf424] | 1146 | which is a recursive sum of product of types, called an \newterm{algebraic data-type}. |
---|
[9a32903] | 1147 | A recursive function is often used to pattern match against a recursive variant type. |
---|
[282061a] | 1148 | \begin{ocaml} |
---|
[6337916] | 1149 | let rec @len_of_string_list@( list : stringList ): int = |
---|
[423c0cd] | 1150 | match list with |
---|
[282061a] | 1151 | Empty -> 0 | |
---|
[6337916] | 1152 | Pair( _ , r ) -> 1 + @len_of_string_list@ r |
---|
[282061a] | 1153 | \end{ocaml} |
---|
[423c0cd] | 1154 | Here, the head of the recursive type is removed and the remainder is processed until the type is empty. |
---|
| 1155 | Each recursion is counted to obtain the number of elements in the type. |
---|
[9a32903] | 1156 | |
---|
[56a8eb8] | 1157 | Note, the compiler statically guarantees that only the correct kind of type is used in the \lstinline[language=OCaml]{match} statement. |
---|
[6337916] | 1158 | However, the union tag is dynamically set on binding (and possible reset on assignment), so a \lstinline[language=OCaml]{match} statement is effectively doing RTTI to select the matching case clause. |
---|
| 1159 | |
---|
| 1160 | In summary, an OCaml variant is a singleton value rather than a set of possibly ordered values, and hence, has no notion of enumerabilty. |
---|
[4da9142] | 1161 | Therefore it is not an enumeration, except for the simple opaque (nullary) case. |
---|
[6337916] | 1162 | |
---|
[c033405] | 1163 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
| 1164 | |
---|
[6337916] | 1165 | \begin{comment} |
---|
| 1166 | Date: Wed, 13 Mar 2024 10:52:34 -0400 |
---|
| 1167 | Subject: Re: OCaml |
---|
| 1168 | To: "Peter A. Buhr" <pabuhr@uwaterloo.ca> |
---|
| 1169 | From: Gregor Richards <gregor.richards@uwaterloo.ca> |
---|
| 1170 | |
---|
| 1171 | On 3/12/24 18:34, Peter A. Buhr wrote: |
---|
| 1172 | > Gregor, attached is a section Jiada wrote on OCaml (1-page). |
---|
| 1173 | > Does it reflect our discussion about functional languages and enumerations? |
---|
| 1174 | |
---|
| 1175 | Yeah, I think so. The most important part, i.e., that once they're |
---|
| 1176 | parameterized they're not really enumerations at all, is covered clearly |
---|
| 1177 | enough. |
---|
| 1178 | |
---|
| 1179 | A couple quibbles: |
---|
| 1180 | |
---|
| 1181 | <<a list of untyped tags>> |
---|
| 1182 | |
---|
| 1183 | This is true, but leaking implementation details. These are nullary datatype |
---|
| 1184 | constructors. Indeed, you later talk about "tagged variants", which are really |
---|
| 1185 | just parameterized variants, using the term "tag" differently, confusing the |
---|
| 1186 | term "tag" further. |
---|
| 1187 | |
---|
[ec20ab9] | 1188 | <<Because week is a summation of values Mon to Sun, it is a sum type in |
---|
[6337916] | 1189 | turns of the functional-programming paradigm>> |
---|
| 1190 | |
---|
| 1191 | It is a *union* of values and is a *union* type. |
---|
| 1192 | |
---|
[7d9a805b] | 1193 | With valediction, |
---|
| 1194 | - Gregor Richards |
---|
| 1195 | |
---|
| 1196 | |
---|
| 1197 | Date: Thu, 14 Mar 2024 21:45:52 -0400 |
---|
| 1198 | Subject: Re: OCaml "enums" do come with ordering |
---|
| 1199 | To: "Peter A. Buhr" <pabuhr@uwaterloo.ca> |
---|
| 1200 | From: Gregor Richards <gregor.richards@uwaterloo.ca> |
---|
| 1201 | |
---|
| 1202 | On 3/14/24 21:30, Peter A. Buhr wrote: |
---|
| 1203 | > I've marked 3 places with your name to shows places with enum ordering. |
---|
| 1204 | > |
---|
[ec20ab9] | 1205 | > type week = Mon | Tue | Wed | Thu | Fri | Sat | Sun |
---|
| 1206 | > let day : week = Mon |
---|
| 1207 | > let take_class( d : week ) = |
---|
[7d9a805b] | 1208 | > if d <= Fri then (* Gregor *) |
---|
[ec20ab9] | 1209 | > Printf.printf "week\n" |
---|
[7d9a805b] | 1210 | > else if d >= Sat then (* Gregor *) |
---|
| 1211 | > Printf.printf "weekend\n"; |
---|
| 1212 | > match d with |
---|
| 1213 | > Mon | Wed -> Printf.printf "CS442\n" | |
---|
| 1214 | > Tue | Thu -> Printf.printf "CS343\n" | |
---|
| 1215 | > Fri -> Printf.printf "Tutorial\n" | |
---|
| 1216 | > _ -> Printf.printf "Take a break\n" |
---|
| 1217 | > |
---|
| 1218 | > let _ = take_class( Mon ); take_class( Sat ); |
---|
| 1219 | > |
---|
| 1220 | > type colour = Red | Green of string | Blue of int * float |
---|
| 1221 | > let c = Red |
---|
| 1222 | > let _ = match c with Red -> Printf.printf "Red, " |
---|
| 1223 | > let c = Green( "abc" ) |
---|
| 1224 | > let _ = match c with Green g -> Printf.printf "%s, " g |
---|
| 1225 | > let c = Blue( 1, 1.5 ) |
---|
| 1226 | > let _ = match c with Blue( i, f ) -> Printf.printf "%d %g\n" i f |
---|
| 1227 | > |
---|
| 1228 | > let check_colour(c: colour): string = |
---|
| 1229 | > if c < Green( "xyz" ) then (* Gregor *) |
---|
| 1230 | > Printf.printf "green\n"; |
---|
| 1231 | > match c with |
---|
| 1232 | > Red -> "Red" | |
---|
| 1233 | > Green g -> g | |
---|
| 1234 | > Blue(i, f) -> string_of_int i ^ string_of_float f |
---|
| 1235 | > let _ = check_colour( Red ); check_colour( Green( "xyz" ) ); |
---|
| 1236 | > |
---|
| 1237 | > type stringList = Empty | Pair of string * stringList |
---|
| 1238 | > let rec len_of_string_list(l: stringList): int = |
---|
| 1239 | > match l with |
---|
| 1240 | > Empty -> 0 | |
---|
| 1241 | > Pair(_ , r) -> 1 + len_of_string_list r |
---|
| 1242 | > |
---|
| 1243 | > let _ = for i = 1 to 10 do |
---|
| 1244 | > Printf.printf "%d, " i |
---|
| 1245 | > done |
---|
| 1246 | > |
---|
| 1247 | > (* Local Variables: *) |
---|
| 1248 | > (* tab-width: 4 *) |
---|
| 1249 | > (* compile-command: "ocaml test.ml" *) |
---|
| 1250 | > (* End: *) |
---|
| 1251 | |
---|
| 1252 | My functional-language familiarity is far more with Haskell than OCaml. I |
---|
| 1253 | mostly view OCaml through a lens of "it's Haskell but with cheating". Haskell |
---|
| 1254 | "enums" (ADTs) aren't ordered unless you specifically and manually put them in |
---|
| 1255 | the Ord typeclass by defining the comparators. Apparently, OCaml has some |
---|
| 1256 | other rule, which I would guess is something like "sort by tag then by order of |
---|
| 1257 | parameter". Having a default behavior for comparators is *bizarre*; my guess |
---|
| 1258 | would be that it gained this behavior in its flirtation with object |
---|
| 1259 | orientation, but that's just a guess (and irrelevant). |
---|
| 1260 | |
---|
| 1261 | This gives a total order, but not enumerability (which would still be |
---|
| 1262 | effectively impossible or even meaningless since enums are just a special case |
---|
| 1263 | of ADTs). |
---|
| 1264 | |
---|
[e00b10d] | 1265 | With valediction, |
---|
| 1266 | - Gregor Richards |
---|
| 1267 | |
---|
| 1268 | Date: Wed, 20 Mar 2024 18:16:44 -0400 |
---|
| 1269 | Subject: Re: |
---|
| 1270 | To: "Peter A. Buhr" <pabuhr@uwaterloo.ca> |
---|
| 1271 | From: Gregor Richards <gregor.richards@uwaterloo.ca> |
---|
| 1272 | |
---|
| 1273 | |
---|
| 1274 | On 3/20/24 17:26, Peter A. Buhr wrote: |
---|
| 1275 | > Gregor, everyone at this end would like a definition of "enumerability". Can |
---|
| 1276 | > you formulate one? |
---|
| 1277 | |
---|
| 1278 | According to the OED (emphasis added to the meaning I'm after): |
---|
| 1279 | |
---|
| 1280 | enumerate (verb, transitive). To count, ascertain the number of; **more |
---|
| 1281 | usually, to mention (a number of things or persons) separately, as if for the |
---|
| 1282 | purpose of counting**; to specify as in a list or catalogue. |
---|
| 1283 | |
---|
| 1284 | With C enums, if you know the lowest and highest value, you can simply loop |
---|
| 1285 | over them in a for loop (this is, of course, why so many enums come with an |
---|
| 1286 | ENUM_WHATEVER_LAST value). But, I would be hesitant to use the word "loop" to |
---|
| 1287 | describe enumerability, since in functional languages, you would recurse for |
---|
| 1288 | such a purpose. |
---|
| 1289 | |
---|
| 1290 | In Haskell, in order to do something with every member of an "enumeration", you |
---|
| 1291 | would have to explicitly list them all. The type system will help a bit since |
---|
| 1292 | it knows if you haven't listed them all, but you would have to statically have |
---|
| 1293 | every element in the enumeration. If somebody added new elements to the |
---|
| 1294 | enumeration later, your code to enumerate over them would no longer work |
---|
| 1295 | correctly, because you can't simply say "for each member of this enumeration do |
---|
| 1296 | X". In Haskell that's because there aren't actually enumerations; what they use |
---|
| 1297 | as enumerations are a degenerate form of algebraic datatypes, and ADTs are |
---|
| 1298 | certainly not enumerable. In OCaml, you've demonstrated that they impose |
---|
| 1299 | comparability, but I would still assume that you can't make a loop over every |
---|
| 1300 | member of an enumeration. (But, who knows!) |
---|
| 1301 | |
---|
| 1302 | Since that's literally what "enumerate" means, it seems like a rather important |
---|
| 1303 | property for enumerations to have ;) |
---|
| 1304 | |
---|
| 1305 | With valediction, |
---|
| 1306 | - Gregor Richards |
---|
| 1307 | |
---|
| 1308 | |
---|
| 1309 | From: Andrew James Beach <ajbeach@uwaterloo.ca> |
---|
| 1310 | To: Gregor Richards <gregor.richards@uwaterloo.ca>, Peter Buhr <pabuhr@uwaterloo.ca> |
---|
| 1311 | CC: Michael Leslie Brooks <mlbrooks@uwaterloo.ca>, Fangren Yu <f37yu@uwaterloo.ca>, |
---|
[c033405] | 1312 | Jiada Liang <j82liang@uwaterloo.ca> |
---|
[e00b10d] | 1313 | Subject: Re: Re: |
---|
| 1314 | Date: Thu, 21 Mar 2024 14:26:36 +0000 |
---|
| 1315 | |
---|
| 1316 | Does this mean that not all enum declarations in C create enumerations? If you |
---|
| 1317 | declare an enumeration like: |
---|
| 1318 | |
---|
| 1319 | enum Example { |
---|
[c033405] | 1320 | Label, |
---|
| 1321 | Name = 10, |
---|
| 1322 | Tag = 3, |
---|
[e00b10d] | 1323 | }; |
---|
| 1324 | |
---|
| 1325 | I don't think there is any way to enumerate (iterate, loop, recurse) over these |
---|
| 1326 | values without listing all of them. |
---|
| 1327 | |
---|
| 1328 | |
---|
| 1329 | Date: Thu, 21 Mar 2024 10:31:49 -0400 |
---|
| 1330 | Subject: Re: |
---|
| 1331 | To: Andrew James Beach <ajbeach@uwaterloo.ca>, Peter Buhr <pabuhr@uwaterloo.ca> |
---|
| 1332 | CC: Michael Leslie Brooks <mlbrooks@uwaterloo.ca>, Fangren Yu <f37yu@uwaterloo.ca>, |
---|
| 1333 | Jiada Liang <j82liang@uwaterloo.ca> |
---|
| 1334 | From: Gregor Richards <gregor.richards@uwaterloo.ca> |
---|
| 1335 | |
---|
| 1336 | I consider this conclusion reasonable. C enums can be nothing more than const |
---|
| 1337 | ints, and if used in that way, I personally wouldn't consider them as |
---|
| 1338 | enumerations in any meaningful sense, particularly since the type checker |
---|
| 1339 | essentially does nothing for you there. Then they're a way of writing consts |
---|
| 1340 | repeatedly with some textual indicator that these definitions are related; more |
---|
| 1341 | namespace, less enum. |
---|
| 1342 | |
---|
| 1343 | When somebody writes bitfield members as an enum, is that *really* an |
---|
| 1344 | enumeration, or just a use of the syntax for enums to keep related definitions |
---|
| 1345 | together? |
---|
| 1346 | |
---|
[4da9142] | 1347 | With valediction, |
---|
| 1348 | - Gregor Richards |
---|
| 1349 | |
---|
| 1350 | |
---|
| 1351 | Date: Tue, 16 Apr 2024 11:04:51 -0400 |
---|
| 1352 | Subject: Re: C unnamed enumeration |
---|
| 1353 | To: "Peter A. Buhr" <pabuhr@uwaterloo.ca> |
---|
| 1354 | CC: <ajbeach@uwaterloo.ca>, <j82liang@uwaterloo.ca>, <mlbrooks@uwaterloo.ca>, |
---|
| 1355 | <f37yu@uwaterloo.ca> |
---|
| 1356 | From: Gregor Richards <gregor.richards@uwaterloo.ca> |
---|
| 1357 | |
---|
| 1358 | On 4/16/24 09:55, Peter A. Buhr wrote: |
---|
| 1359 | > So what is a variant? Is it a set of tag names, which might be a union or is it |
---|
| 1360 | > a union, which might have tag names? |
---|
| 1361 | |
---|
| 1362 | Your tagless variant bears no resemblance to variants in any functional |
---|
| 1363 | programming language. A variant is a tag AND a union. You might not need to put |
---|
| 1364 | anything in the union, in which case it's a pointless union, but the named tag |
---|
| 1365 | is absolutely mandatory. That's the thing that varies. |
---|
| 1366 | |
---|
| 1367 | I was unaware of std::variant. As far as functional languages are concerned, |
---|
| 1368 | std::variant IS NOT A VARIANT. Perhaps it would be best to use the term ADT for |
---|
| 1369 | the functional language concept, because that term has no other meanings. |
---|
| 1370 | |
---|
| 1371 | An ADT cannot not have a named tag. That's meaningless. The tag is the data |
---|
| 1372 | constructor, which is the thing you actually define when you define an ADT. It |
---|
| 1373 | is strictly the union that's optional. |
---|
| 1374 | |
---|
[6337916] | 1375 | With valediction, |
---|
| 1376 | - Gregor Richards |
---|
| 1377 | \end{comment} |
---|
[223b631] | 1378 | |
---|
| 1379 | |
---|
| 1380 | \section{Comparison} |
---|
| 1381 | |
---|
[7d9a805b] | 1382 | \VRef[Table]{t:FeatureLanguageComparison} shows a comparison of enumeration features and programming languages. |
---|
| 1383 | The features are high level and may not capture nuances within a particular language |
---|
| 1384 | The @const@ feature is simple macros substitution and not a typed enumeration. |
---|
| 1385 | |
---|
| 1386 | \begin{table} |
---|
| 1387 | \caption{Enumeration Feature / Language Comparison} |
---|
| 1388 | \label{t:FeatureLanguageComparison} |
---|
| 1389 | \small |
---|
| 1390 | \setlength{\tabcolsep}{3pt} |
---|
| 1391 | \newcommand{\CM}{\checkmark} |
---|
| 1392 | \begin{tabular}{r|c|c|c|c|c|c|c|c|c|c|c|c|c} |
---|
| 1393 | &Pascal & Ada &\Csharp& OCaml & Java &Modula-3&Golang& Rust & Swift & Python& C & \CC & \CFA \\ |
---|
| 1394 | \hline |
---|
| 1395 | @const@ & \CM & & & & & & \CM & & & & & \CM & \\ |
---|
| 1396 | \hline |
---|
| 1397 | \hline |
---|
[4da9142] | 1398 | opaque & & & & & & & & & & & & & \CM \\ |
---|
[7d9a805b] | 1399 | \hline |
---|
| 1400 | typed & & & & & & & & & & & @int@ & integral & @T@ \\ |
---|
| 1401 | \hline |
---|
| 1402 | safe & & & & & & & & & & & & \CM & \CM \\ |
---|
| 1403 | \hline |
---|
| 1404 | ordered & & & & & & & & & & & \CM & \CM & \CM \\ |
---|
| 1405 | \hline |
---|
| 1406 | dup. values & & & & & & & & & & alias & \CM & \CM & \CM \\ |
---|
| 1407 | \hline |
---|
| 1408 | setable & & & & & & & & & & & \CM & \CM & \CM \\ |
---|
| 1409 | \hline |
---|
| 1410 | auto-init & & & & & & & & & & & \CM & \CM & \CM \\ |
---|
| 1411 | \hline |
---|
[c033405] | 1412 | (Un)Scoped & & & & & & & & & & & U & U/S & U/S \\ |
---|
[7d9a805b] | 1413 | \hline |
---|
| 1414 | overload & & \CM & & & & & & & & & & \CM & \CM \\ |
---|
| 1415 | \hline |
---|
| 1416 | switch & & & & & & & & & & & \CM & \CM & \CM \\ |
---|
| 1417 | \hline |
---|
| 1418 | loop & & & & & & & & & & & & & \CM \\ |
---|
| 1419 | \hline |
---|
[ec20ab9] | 1420 | array/subscript & & & & & & & & & & & \CM & & \CM \\ |
---|
[7d9a805b] | 1421 | \hline |
---|
| 1422 | subtype & & & & & & & & & & & & & \CM \\ |
---|
[223b631] | 1423 | \hline |
---|
[7d9a805b] | 1424 | inheritance & & & & & & & & & & & & & \CM \\ |
---|
[223b631] | 1425 | \end{tabular} |
---|
[7d9a805b] | 1426 | \end{table} |
---|