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