Ignore:
Timestamp:
Aug 2, 2024, 12:32:18 AM (5 weeks ago)
Author:
JiadaL <j82liang@…>
Branches:
master
Children:
a9ae5ca, b12e4ad
Parents:
3a7cd15
Message:

Update

File:
1 edited

Legend:

Unmodified
Added
Removed
  • doc/theses/jiada_liang_MMath/CEnum.tex

    r3a7cd15 rc141c09  
    1 \chapter{C Enum in CFA}
     1\chapter{C Enumeration in CFA}
    22
    3 \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.
    4 C style enums in \CFA language are called "C enum".
    5 \CFA also extends C-Style enumeration by adding a number of new features that bring enumerations inline with other modern programming languages.
     3\CFA supports legacy C enumeration using the same syntax for backwards compatibility.
     4C-style Enumeration in \CFA language are called \newterm{C Enumeration} or \newterm{C Enum}.
     5The 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.
    67Any enumeration extensions must be intuitive to C programmers both in syntax and semantics.
    78The following sections detail all of my new contributions to enumerations in \CFA.
     
    2425enum E1 { First, Second, Third, Fourth };
    2526enum E2 { @Fourth@, @Third@, @Second@, @First@ }; $\C{// same enumerator names}$
    26 E1 f() { return Third; }                                $\C{// overloaded functions, different return types}$
     27E1 f() { return Third; }                                $\C{// overload functions with different return types}$
    2728E2 f() { return Fourth; }
    2829void g( E1 e );
     
    7172\section{Type Safety}
    7273
    73 As in Section~\ref{s:Usage}, C's implicit conversion between enumeration and integral type raises a safety concern.
    74 \CFA disallows an implicit conversion from integral type to enueration, and conversion between different C enumeration type.
    75 It loses some degree of its backward compatibility to C, in exchange for type safety.
     74As in Section~\ref{s:Usage}, C's implicit bidirectional conversion between enumeration and integral type raises a safety concern.
     75In \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.
     78an enumeration can be implicitly converted into an integral type, with an associated @safe@ conversion cost.
     79But an integral type cannot be implicitly converted into a C enumeration. (Conversion Cost is Infinity.)
    7680\begin{cfa}
    7781enum Bird { Pengin, Robin, Eagle };
     
    8185@enum Bird bird = Shark;@ $\C{// Disallow }$
    8286@enum Bird bird = 1;@  $\C{// Disallow }$
    83 
     87\end{cfa}
     88As 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,
     89in 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}
    8491@enum Bird bird = (Bird) Shark@
    8592@enum Bird bird = (Bird) 1;@
    8693\end{cfa}
    87 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,
    88 in which case \CFA treats C enuemration as its underlying integral type. In such cases, it is up to user to ensure program correctness.
Note: See TracChangeset for help on using the changeset viewer.