\chapter{C Enum in CFA}

\CFA supports C enumeration using the same syntax for backwards compatibility, and the semantics is mostly consistent with C with small difference in terms of typing.
C style enums in \CFA language are called "C enum".
\CFA also extends C-Style enumeration by adding a number of new features that bring enumerations inline with other modern programming languages.
Any enumeration extensions must be intuitive to C programmers both in syntax and semantics.
The following sections detail all of my new contributions to enumerations in \CFA.

\section{Enumerator Visibility}
\label{s:EnumeratorVisibility}

In C, unscoped enumerators present a \newterm{naming problem} when multiple enumeration types appear in the same scope with duplicate enumerator names.
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.

The \CFA type-system allows extensive overloading, including enumerators.
Furthermore, \CFA uses the environment, such as the left-hand of assignment and function arguments, to pinpoint the best overloaded name.
\VRef[Figure]{f:EnumeratorVisibility} shows enumeration overloading and how qualification and casting are used to disambiguate ambiguous situations.
\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.
Experience from \CFA developers is that the type system implicitly and correctly disambiguates the majority of overloaded names.
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.

\begin{figure}
\begin{cfa}
enum E1 { First, Second, Third, Fourth };
enum E2 { @Fourth@, @Third@, @Second@, @First@ }; $\C{// same enumerator names}$
E1 f() { return Third; }				$\C{// overloaded functions, different return types}$
E2 f() { return Fourth; }
void g( E1 e );
void h( E2 e );
void foo() {							$\C{// different resolutions and dealing with ambiguities}$
	E1 e1 = First;   E2 e2 = First;		$\C{// initialization}$
	e1 = Second;   e2 = Second;			$\C{// assignment}$
	e1 = f();   e2 = f();				$\C{// function return}$
	g( First );   h( First );			$\C{// function argument}$
	int i = @E1.@First + @E2.@First;	$\C{// disambiguate with qualification}$
	int j = @(E1)@First + @(E2)@First;	$\C{// disambiguate with cast}$
}
\end{cfa}
\caption{Enumerator Visibility and Disambiguating}
\label{f:EnumeratorVisibility}
\end{figure}

\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.
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.
Any ambiguity can be resolved using qualification or casting.


\section{Enumerator Scoping}

An enumeration can be scoped, using @'!'@, so the enumerator constants are not projected into the enclosing scope.
\begin{cfa}
enum Week @!@ { Mon, Tue, Wed, Thu = 10, Fri, Sat, Sun };
enum RGB @!@ { Red, Green, Blue };
\end{cfa}
Now the enumerators \emph{must} be qualified with the associated enumeration type.
\begin{cfa}
Week week = @Week.@Mon;
week = @Week.@Sat;
RGB rgb = @RGB.@Red;
rgb = @RGB.@Blue;
\end{cfa}
{\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}).
\begin{cfa}
with ( @Week@, @RGB@ ) {				$\C{// type names}$
	 week = @Sun@;						$\C{// no qualification}$
	 rgb = @Green@;
}
\end{cfa}
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.

\section{Type Safety}

As in Section~\ref{s:Usage}, C's implicit conversion between enumeration and integral type raises a safety concern.
\CFA disallows an implicit conversion from integral type to enueration, and conversion between different C enumeration type.
It loses some degree of its backward compatibility to C, in exchange for type safety.
\begin{cfa}
enum Bird { Pengin, Robin, Eagle };
enum Fish { Shark, Salmon, Whale };

int i = Robin; $\C{// Allow, implicitly converts to 1}$ 
@enum Bird bird = Shark;@ $\C{// Disallow }$ 
@enum Bird bird = 1;@  $\C{// Disallow }$ 

@enum Bird bird = (Bird) Shark@ 
@enum Bird bird = (Bird) 1;@ 
\end{cfa}
As a workaround, \CFA allows explicit cast to an enumeration, turning an integral type to an enumeration that can be used in assignment or function argument, 
in which case \CFA treats C enuemration as its underlying integral type. In such cases, it is up to user to ensure program correctness.