[c141c09] | 1 | \chapter{C Enumeration in CFA} |
---|
[dd78dbc] | 2 | |
---|
[c141c09] | 3 | \CFA supports legacy C enumeration using the same syntax for backwards compatibility. |
---|
| 4 | C-style Enumeration in \CFA language are called \newterm{C Enumeration} or \newterm{C Enum}. |
---|
| 5 | The semantics of C Enumeration is mostly consistent with C with more restrictive typing. |
---|
| 6 | \CFA also extends C Enumeration by adding a number of new features that bring enumerations aligns with other modern programming languages. |
---|
[dd78dbc] | 7 | Any enumeration extensions must be intuitive to C programmers both in syntax and semantics. |
---|
| 8 | The following sections detail all of my new contributions to enumerations in \CFA. |
---|
| 9 | |
---|
| 10 | \section{Enumerator Visibility} |
---|
| 11 | \label{s:EnumeratorVisibility} |
---|
| 12 | |
---|
| 13 | In C, unscoped enumerators present a \newterm{naming problem} when multiple enumeration types appear in the same scope with duplicate enumerator names. |
---|
| 14 | There is no mechanism in C to resolve these naming conflicts other than renaming one of the duplicates, which may be impossible if the conflict comes from system include files. |
---|
| 15 | |
---|
| 16 | The \CFA type-system allows extensive overloading, including enumerators. |
---|
| 17 | Furthermore, \CFA uses the environment, such as the left-hand of assignment and function arguments, to pinpoint the best overloaded name. |
---|
| 18 | \VRef[Figure]{f:EnumeratorVisibility} shows enumeration overloading and how qualification and casting are used to disambiguate ambiguous situations. |
---|
| 19 | \CFA overloading allows programmers to use the most meaningful names without fear of name clashes within a program or from external sources, like include files. |
---|
| 20 | Experience from \CFA developers is that the type system implicitly and correctly disambiguates the majority of overloaded names. |
---|
| 21 | That is, it is rare to get an incorrect selection or ambiguity, even among hundreds of overloaded variables and functions, that requires disambiguation using qualification or casting. |
---|
| 22 | |
---|
| 23 | \begin{figure} |
---|
| 24 | \begin{cfa} |
---|
| 25 | enum E1 { First, Second, Third, Fourth }; |
---|
| 26 | enum E2 { @Fourth@, @Third@, @Second@, @First@ }; $\C{// same enumerator names}$ |
---|
[c141c09] | 27 | E1 f() { return Third; } $\C{// overload functions with different return types}$ |
---|
[dd78dbc] | 28 | E2 f() { return Fourth; } |
---|
| 29 | void g( E1 e ); |
---|
| 30 | void h( E2 e ); |
---|
| 31 | void foo() { $\C{// different resolutions and dealing with ambiguities}$ |
---|
| 32 | E1 e1 = First; E2 e2 = First; $\C{// initialization}$ |
---|
| 33 | e1 = Second; e2 = Second; $\C{// assignment}$ |
---|
| 34 | e1 = f(); e2 = f(); $\C{// function return}$ |
---|
| 35 | g( First ); h( First ); $\C{// function argument}$ |
---|
| 36 | int i = @E1.@First + @E2.@First; $\C{// disambiguate with qualification}$ |
---|
| 37 | int j = @(E1)@First + @(E2)@First; $\C{// disambiguate with cast}$ |
---|
| 38 | } |
---|
| 39 | \end{cfa} |
---|
| 40 | \caption{Enumerator Visibility and Disambiguating} |
---|
| 41 | \label{f:EnumeratorVisibility} |
---|
| 42 | \end{figure} |
---|
| 43 | |
---|
| 44 | \CFA overloading allows programmers to use the most meaningful names without fear of name clashes within a program or from external sources, like include files. |
---|
| 45 | Experience from \CFA developers is that the type system implicitly and correctly disambiguates the majority of overloaded names, \ie it is rare to get an incorrect selection or ambiguity, even among hundreds of overloaded variables and functions. |
---|
| 46 | Any ambiguity can be resolved using qualification or casting. |
---|
| 47 | |
---|
| 48 | |
---|
| 49 | \section{Enumerator Scoping} |
---|
| 50 | |
---|
| 51 | An enumeration can be scoped, using @'!'@, so the enumerator constants are not projected into the enclosing scope. |
---|
| 52 | \begin{cfa} |
---|
| 53 | enum Week @!@ { Mon, Tue, Wed, Thu = 10, Fri, Sat, Sun }; |
---|
| 54 | enum RGB @!@ { Red, Green, Blue }; |
---|
| 55 | \end{cfa} |
---|
| 56 | Now the enumerators \emph{must} be qualified with the associated enumeration type. |
---|
| 57 | \begin{cfa} |
---|
| 58 | Week week = @Week.@Mon; |
---|
| 59 | week = @Week.@Sat; |
---|
| 60 | RGB rgb = @RGB.@Red; |
---|
| 61 | rgb = @RGB.@Blue; |
---|
| 62 | \end{cfa} |
---|
| 63 | {\color{red}@***@}It is possible to toggle back to unscoped using the \CFA @with@ clause/statement (see also \CC \lstinline[language=c++]{using enum} in Section~\ref{s:C++RelatedWork}). |
---|
| 64 | \begin{cfa} |
---|
| 65 | with ( @Week@, @RGB@ ) { $\C{// type names}$ |
---|
| 66 | week = @Sun@; $\C{// no qualification}$ |
---|
| 67 | rgb = @Green@; |
---|
| 68 | } |
---|
| 69 | \end{cfa} |
---|
| 70 | As in Section~\ref{s:EnumeratorVisibility}, opening multiple scoped enumerations in a @with@ can result in duplicate enumeration names, but \CFA implicit type resolution and explicit qualification/casting handle this localized scenario. |
---|
| 71 | |
---|
| 72 | \section{Type Safety} |
---|
| 73 | |
---|
[c141c09] | 74 | As in Section~\ref{s:Usage}, C's implicit bidirectional conversion between enumeration and integral type raises a safety concern. |
---|
| 75 | In \CFA, the conversion is unidirectional: |
---|
| 76 | % disallows an implicit conversion from integral type to enumeration, and conversion between different C enumeration type. |
---|
| 77 | % It loses some degree of its backward compatibility to C, in exchange for type safety. |
---|
| 78 | an enumeration can be implicitly converted into an integral type, with an associated @safe@ conversion cost. |
---|
| 79 | But an integral type cannot be implicitly converted into a C enumeration. (Conversion Cost is Infinity.) |
---|
[dd78dbc] | 80 | \begin{cfa} |
---|
| 81 | enum Bird { Pengin, Robin, Eagle }; |
---|
| 82 | enum Fish { Shark, Salmon, Whale }; |
---|
| 83 | |
---|
| 84 | int i = Robin; $\C{// Allow, implicitly converts to 1}$ |
---|
| 85 | @enum Bird bird = Shark;@ $\C{// Disallow }$ |
---|
| 86 | @enum Bird bird = 1;@ $\C{// Disallow }$ |
---|
[c141c09] | 87 | \end{cfa} |
---|
| 88 | As a workaround, \CFA allows an explicit cast to an enumeration, turning an integral type to an enumeration that can be used in assignment or function argument, |
---|
| 89 | in which case \CFA treats C enuemration as its underlying integral type. In such cases, it is up to user to ensure program correctness. |
---|
| 90 | \begin{cfa} |
---|
[dd78dbc] | 91 | @enum Bird bird = (Bird) Shark@ |
---|
| 92 | @enum Bird bird = (Bird) 1;@ |
---|
[c141c09] | 93 | \end{cfa} |
---|