Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • doc/proposals/enum.tex

    rd63746f re11cdc0  
    126126
    127127Specifically, an enumerated type restricts its values to a fixed set of named constants.
    128 While all types are restricted to a fixed set of values because of the underlying von Neumann architecture, and hence, to a corresponding set of constants, e.g., @3@, @3.5@, @3.5+2.1i@, @'c'@, @"abc"@, etc., these values are not named, other than the programming-language supplied constant names.
    129 
    130 Fundamentally, all enumeration systems have an \newterm{enumeration} type with an associated set of \newterm{enumerator} names.
    131 An enumeration has three universal attributes, \newterm{position}, \newterm{label}, and \newterm{value}, as shown by this representative enumeration, where position and value can be different.
     128While all types are restricted to a fixed set of values because of the underlying von Neumann architecture, and hence, to a corresponding set of constants, e.g., @3@, @3.5@, @3.5+2.1i@, @'c'@, @"abc"@, etc., these values are not named, other than the programming-language supplied constants.
     129
     130Fundamentally, all enumeration systems have an \newterm{enumeration} type with its associated \newterm{enumerator} constants.
     131These components have three universal attributes, \newterm{position}, \newterm{label}, and \newterm{value}, as shown by this representative enumeration, where position and value can be different.
    132132\begin{cquote}
    133133\small\sf\setlength{\tabcolsep}{3pt}
     
    168168This feature allow enumerator lines to be interchanged without moving a comma.\footnote{
    169169A terminating comma appears in other C syntax, e.g., the initializer list.}
    170 Finally, C enumerators are \newterm{unscoped}, i.e., enumerators declared inside of an @enum@ are visible (projected) into the enclosing scope of the @enum@ type.
     170Finally, C enumerators are \newterm{unscoped}, i.e., enumerators declared inside of an @enum@ are visible (projected) in the enclosing scope of the @enum@ type.
    171171
    172172In theory, a C enumeration \emph{variable} is an implementation-defined integral type large enough to hold all enumerated values.
     
    208208
    209209In C, unscoping of enumerators presents a \newterm{naming problem} when multiple enumeration types appear in the same scope with duplicate enumerator names.
    210 There is no mechanism in C to resolve these naming conflicts other than renaming of one of the duplicates, which may be impossible.
     210There is no mechanism in C to resolve these naming conflicts other than renaming of one of the duplicates, which may not be possible.
    211211
    212212The \CFA type-system allows extensive overloading, including enumerators.
     
    221221        C1 e1 = First;   C2 e2 = First;
    222222        e1 = Second;   e2 = Second;
    223         e1 = p();   e2 = p();                           $\C{// correctly resolved function call}$
    224         int i = @C1.@First + @C2.@First;        $\C{// ambiguous without qualification}$
     223        e1 = p();   e2 = p();
     224        e1 = @C1.@First + @C1.@First;           $\C{// ambiguous without qualification (and dangerous)}$
    225225}
    226226\end{lstlisting}
    227 \CFA overloading allows programmers to use the most meaningful names without fear of unresolvable clashes from included files, which are correctable with qualification.
     227\CFA overloading allows programmers to use the most meaningful names with little fear of unresolvable clashes from included files, which can always be corrected.
    228228
    229229
     
    235235enum( char * ) Names @!@ { /* as above */ };
    236236\end{lstlisting}
    237 Now the enumerators \emph{must} be qualified with the associated enumeration.
     237Now the enumerators must be qualified with the associated enumeration.
    238238\begin{lstlisting}
    239239Weekday weekday = @Weekday@.Monday;
     
    241241names = @Names.@Jane;
    242242\end{lstlisting}
    243 It is possible to toggle back to unscoping using the \CFA @with@ clause/statement (see also \CC \lstinline[language=c++]{using enum} in Section~\ref{s:C++RelatedWork}).
     243It is possible to toggle back to unscoping using the \CFA @with@ clause/statement.
    244244\begin{lstlisting}
    245245Weekday weekday;
     
    250250}
    251251\end{lstlisting}
    252 As in Section~\ref{s:EnumeratorNameResolution}, opening multiple unscoped enumerations can result in duplicate enumeration names, but \CFA type resolution and falling back to explicit qualification handles name resolution.
     252As in section~\ref{s:EnumeratorNameResolution}, opening multiple unscoped enumerations can result in duplicate enumeration names, but \CFA type resolution and falling back to explicit qualification handles ambiguities.
     253
    253254
    254255\subsection{Enumerator Typing}
     
    257258Figure~\ref{f:EumeratorTyping} shows a series of examples illustrating that all \CFA types can be use with an enumeration and each type's constants used to set the enumerator constants.
    258259Note, the synonyms @Liz@ and @Beth@ in the last declaration.
    259 
    260 Because enumerators are constants, the enumeration type is implicitly @const@, so all the enumerator types in Figure~\ref{f:EumeratorTyping} are rewritten with @const@.
    261 A typed enumeration has an implicit (safe) conversion to its base type.
    262 \begin{lstlisting}
    263 char currency = Dollar;
    264 string fred = Fred;                                             $\C{// implicit conversion from char * to \CFA string type}$
    265 Person student = Beth;
    266 \end{lstlisting}
    267260
    268261% \begin{lstlisting}[label=lst:color]
     
    324317};
    325318\end{lstlisting}
    326 Note, the enumeration type can be a structure (see @Person@ in Figure~\ref{f:EumeratorTyping}), so it is possible to have the equivalent of multiple arrays of companion data using an array of structures.
    327 
    328319
    329320\subsection{Pure Enumerators}
    330321
    331 An empty enumerator type, @enum()@, implies the enumerators are pure symbols without values but set properties;
     322An empty type, @enum()@, implies the enumerators are pure symbols without values;
    332323hence, there is no default conversion to @int@.
    333324
     
    335326enum() Mode { O_RDONLY, O_WRONLY, O_CREAT, O_TRUNC, O_APPEND };
    336327@***@Mode iomode = O_RDONLY;
    337 bool b = iomode == O_RDONLY || iomode < O_APPEND;
    338328int i = iomode;                                                 $\C{\color{red}// disallowed}$
    339329\end{lstlisting}
     
    343333If follows from enumerator typing that the enumerator type can be another enumerator.
    344334\begin{lstlisting}
    345 enum( @char@ ) Currency { Dollar = '$\textdollar$', Euro = '$\texteuro$', Pound = '$\textsterling$'  };
    346 enum( @Currency@ ) Europe { Euro = Currency.Euro, Pound = Currency.Pound }; // intersection
    347335enum( char ) Letter { A = 'A',  B = 'B', C = 'C', ..., Z = 'Z' };
    348 enum( @Letter@ ) Greek { Alph = A, Beta = B, ..., Zeta = Z }; // intersection
    349 \end{lstlisting}
    350 Subset enumerations may have more or less enumerators than their typed enumeration, but the enumerator values must be from the typed enumeration.
    351 For example, @Greek@ enumerators are a subset of type @Letter@ and are type compatible with enumeration @Letter@, but @Letter@ enumerators are not type compatible with enumeration @Greek@.
     336enum( @Letter@ ) Greek { Alph = A, Beta = B, ..., Zeta = Z }; // alphabet intersection
     337\end{lstlisting}
     338Enumeration @Greek@ may have more or less enumerators than @Letter@, but the enumerator values must be from @Letter@.
     339Therefore, @Greek@ enumerators are a subset of type @Letter@ and are type compatible with enumeration @Letter@, but @Letter@ enumerators are not type compatible with enumeration @Greek@.
    352340\begin{lstlisting}
    353341Letter letter = A;
     
    360348\subsection{Enumeration Inheritance}
    361349
    362 \CFA Plan-9 inheritance may be used with enumerations, where Plan-9 inheritance is containment inheritance with implicit unscoping (like a nested unnamed @struct@/@union@ in C).
     350\CFA Plan-9 inheritance may be used with enumerations.
    363351\begin{lstlisting}
    364352enum( char * ) Names { /* as above */ };
     
    394382\end{tabular}
    395383\end{cquote}
    396 Note, the validity of calls is the same for call-by-reference as for call-by-value, and @const@ restrictions are the same as for other types.
     384Note, the validity of calls is the same for call-by-reference as for call-by-value, and const restrictions are the same as for other types.
    397385
    398386
    399387\subsection{Enumeration Pseudo-functions}
    400388
    401 Pseudo-functions are function-like operators that do not result in any run-time computations, i.e., like @sizeof@, @offsetof@, @typeof@.
     389Pseudo-functions are function-like operators that do not result in any run-time computations, i.e., like @sizeof@.
    402390Often a call to a pseudo-function is substituted with information extracted from the symbol table at compilation time, like storage size or alignment associated with the underlying architecture..
    403391
     392
     393\subsubsection{Enumerator Attributes}
    404394The attributes of an enumerator are accessed by pseudo-functions @position@, @value@, and @label@.
    405395\begin{lstlisting}
    406 @***@int jane_pos = @position@( Names.Jane );   $\C{// 2}$
    407 @***@char * jane_value = @value@( Names.Jane ); $\C{// "JANE"}$
    408 @***@char * jane_label = @label@( Names.Jane ); $\C{// "Jane"}$
    409 sout | @label@( Names.Jane ) | @value@( Names.Jane );
    410 \end{lstlisting}
    411 Note the ability to print both enumerator label and value.
    412 
    413 
    414 \subsection{Enumerator Position or Value}
    415 
    416 Enumerators can be used in multiple contexts.
    417 In most programming languages, an enumerator is implicitly converted to its value (like a typed macro substitution).
    418 However, enumerator synonyms and typed enumerations make this implicit conversion to value incorrect in some contexts.
    419 In these contexts, a programmer's initition assumes an implicit conversion to postion.
    420 
    421 For example, an intuitive use of enumerations is with the \CFA @switch@/@choose@ statement, where @choose@ performs an implict @break@ rather than a fall-through at the end of a @case@ clause.
    422 \begin{cquote}
     396@***@int jane_pos = position( Names.Jane );   $\C{// 2}$
     397@***@char * jane_value = value( Names.Jane ); $\C{// "JANE"}$
     398@***@char * jane_label = label( Names.Jane ); $\C{// "Jane"}$
     399\end{lstlisting}
     400
     401% An instance of \CFA-enum (denoted as @<enum_instance>@) is a label for the defined enum name.
     402% The label can be retrieved by calling the function @label( <enum_instance> )@.
     403% Similarly, the @value()@ function returns the value used to initialize the \CFA-enum.
     404
     405
     406\subsubsection{\lstinline{switch/choose} Statement}
     407
     408An intuitive use of enumerations is with the \CFA @switch@/@choose@ statement, where @choose@ performs an implict @break@ rather than a fall-through at the end of a @case@ clause.
    423409\begin{lstlisting}
    424410enum Count { First, Second, Third, Fourth };
    425411Count e;
    426412\end{lstlisting}
     413\begin{cquote}
    427414\begin{tabular}{ll}
    428415\begin{lstlisting}
    429 
    430416choose( e ) {
    431417        case @First@: ...;
     
    437423&
    438424\begin{lstlisting}
    439 // rewrite
    440425choose( @value@( e ) ) {
    441426        case @value@( First ): ...;
     
    447432\end{tabular}
    448433\end{cquote}
    449 Here, the intuitive code on the left is implicitly transformed into the statndard implementation on the right, using the value of the enumeration variable and enumerators.
     434Here, the intuitive implementation on the right uses the value of the enumeration and enumerators.
    450435However, this implementation is fragile, e.g., if the enumeration is changed to:
    451436\begin{lstlisting}
    452437enum Count { First, Second, Third @= First@, Fourth };
    453438\end{lstlisting}
    454 which make @Third == First@ and @Fourth == Second@, causing a compilation error because of duplicase @case@ clauses.
    455 To better match with programmer intuition, \CFA toggles between value and position semantics depneding on the language context.
    456 For conditional clauses and switch statments, \CFA uses the robust position implementation.
     439which make @Third == First@ and @Fourth == Second@, causing duplicase @case@ clauses.
     440To better match with programmer intuition, \CFA uses a more robust implementation form when the type of a @switch@ expression is an enumeration.
    457441\begin{lstlisting}
    458442choose( @position@( e ) ) {
     
    12931277
    12941278\subsection{\CC}
    1295 \label{s:C++RelatedWork}
    1296 
    1297 \CC is backwards compatible with C, so it inherited C's enumerations.
    1298 However, the following non-backwards compatible changes have been made.
    1299 \begin{quote}
    1300 7.2 Change: \CC objects of enumeration type can only be assigned values of the same enumeration type.
    1301 In C, objects of enumeration type can be assigned values of any integral type. \\
    1302 Example:
    1303 \begin{lstlisting}
    1304 enum color { red, blue, green };
    1305 color c = 1;                                                    $\C{// valid C, invalid C++}$
    1306 \end{lstlisting}
    1307 \textbf{Rationale}: The type-safe nature of C++. \\
    1308 \textbf{Effect on original feature}: Deletion of semantically well-defined feature. \\
    1309 \textbf{Difficulty of converting}: Syntactic transformation. (The type error produced by the assignment can be automatically corrected by applying an explicit cast.) \\
    1310 \textbf{How widely used}: Common.
    1311 \end{quote}
    1312 \begin{quote}
    1313 7.2 Change: In \CC, the type of an enumerator is its enumeration.
    1314 In C, the type of an enumerator is @int@. \\
    1315 Example:
    1316 \begin{lstlisting}
    1317 enum e { A };
    1318 sizeof(A) == sizeof(int)                                $\C{// in C}$
    1319 sizeof(A) == sizeof(e)                                  $\C{// in C++}$
    1320 /* and sizeof(int) is not necessary equal to sizeof(e) */
    1321 \end{lstlisting}
    1322 \textbf{Rationale}: In C++, an enumeration is a distinct type. \\
    1323 \textbf{Effect on original feature}: Change to semantics of well-defined feature. \\
    1324 \textbf{Difficulty of converting}: Semantic transformation. \\
    1325 \textbf{How widely used}: Seldom. The only time this affects existing C code is when the size of an enumerator is taken.
    1326 Taking the size of an enumerator is not a common C coding practice.
    1327 \end{quote}
    1328 Hence, the values in a \CC enumeration can only be its enumerators (without a cast).
    1329 While the storage size of an enumerator is up to the compiler, there is still an implicit cast to @int@.
    1330 \begin{lstlisting}
    1331 enum E { A, B, C };
    1332 E e = A;
    1333 int i = A;   i = e;                                             $\C{// implicit casts to int}$
    1334 \end{lstlisting}
    1335 \CC{11} added a scoped enumeration, \lstinline[language=c++]{enum class} (or \lstinline[language=c++]{enum struct}), so the enumerators are local to the enumeration and must be accessed using type qualification.
     1279
     1280\CC is backwards compatible with C, so it inherited C's enumerations, except there is no implicit conversion from an integral value to an enumeration;
     1281hence, the values in a \CC enumeration can only be its enumerators (without a cast).
     1282There is no mechanism to iterate through an enumeration.
     1283
     1284\CC{11} added a scoped enumeration, \lstinline[language=c++]{enum class} (or \lstinline[language=c++]{enum struct}), so the enumerators are local to the enumeration and must be accessed using type qualification, e.g., @Weekday::Monday@.
     1285\CC{20} supports unscoped access with a \lstinline[language=c++]{using enum} declaration.
     1286
     1287For both unscoped and scoped enumerations, 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.
     1288In \CC{11}, the underlying integral type can be explicitly specified:
    13361289\begin{lstlisting}[language=c++,{moredelim=**[is][\color{red}]{@}{@}}]
    1337 enum class E { A, B, C };
    1338 E e = @E::@A;                                                   $\C{// qualified enumerator}$
    1339 e = B;                                                                  $\C{// B not in scope}$
    1340 \end{lstlisting}
    1341 \CC{20} supports unscoped access with a \lstinline[language=c++]{using enum} declaration.
    1342 \begin{lstlisting}[language=c++,{moredelim=**[is][\color{red}]{@}{@}}]
    1343 enum class E { A, B, C };
    1344 @using enum E;@
    1345 E e = A;                                                                $\C{// direct access}$
    1346 e = B;                                                                  $\C{// direct access}$
    1347 \end{lstlisting}
    1348 \CC{11} added the ability to explicitly declare the underlying integral type for \lstinline[language=c++]{enum class}.
    1349 \begin{lstlisting}[language=c++,{moredelim=**[is][\color{red}]{@}{@}}]
    1350 enum class RGB @: long@ { Red, Green, Blue };
    1351 enum class rgb @: char@ { Red = 'r', Green = 'g', Blue = 'b' };
    1352 enum class srgb @: signed char@ { Red = -1, Green = 0, Blue = 1 };
    1353 \end{lstlisting}
    1354 There is no implicit conversion from the \lstinline[language=c++]{enum class} type and to its type.
    1355 \begin{lstlisting}[language=c++,{moredelim=**[is][\color{red}]{@}{@}}]
    1356 rgb crgb = rgb::Red;
    1357 char ch = rgb::Red;   ch = crgb;                $\C{// disallowed}$
    1358 \end{lstlisting}
    1359 Finally, there is no mechanism to iterate through an enumeration nor use the enumeration type to declare an array dimension.
    1360 
     1290enum class RGB : @long@ { Red, Green, Blue };
     1291enum class rgb : @char@ { Red = 'r', Green = 'g', Blue = 'b' };
     1292enum class srgb : @signed char@ { Red = -1, Green = 0, Blue = 1 };
     1293RGB colour1 = @RGB::@Red;
     1294rgb colour2 = @rgb::@Red;
     1295srgb colour3 = @srgb::@Red;
     1296\end{lstlisting}
    13611297
    13621298\subsection{Go}
Note: See TracChangeset for help on using the changeset viewer.