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