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