Changeset c141c09 for doc/theses/jiada_liang_MMath/CEnum.tex
- Timestamp:
- Aug 2, 2024, 12:32:18 AM (5 weeks ago)
- Branches:
- master
- Children:
- a9ae5ca, b12e4ad
- Parents:
- 3a7cd15
- 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} 2 2 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. 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. 6 7 Any enumeration extensions must be intuitive to C programmers both in syntax and semantics. 7 8 The following sections detail all of my new contributions to enumerations in \CFA. … … 24 25 enum E1 { First, Second, Third, Fourth }; 25 26 enum E2 { @Fourth@, @Third@, @Second@, @First@ }; $\C{// same enumerator names}$ 26 E1 f() { return Third; } $\C{// overload ed functions,different return types}$27 E1 f() { return Third; } $\C{// overload functions with different return types}$ 27 28 E2 f() { return Fourth; } 28 29 void g( E1 e ); … … 71 72 \section{Type Safety} 72 73 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. 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.) 76 80 \begin{cfa} 77 81 enum Bird { Pengin, Robin, Eagle }; … … 81 85 @enum Bird bird = Shark;@ $\C{// Disallow }$ 82 86 @enum Bird bird = 1;@ $\C{// Disallow }$ 83 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} 84 91 @enum Bird bird = (Bird) Shark@ 85 92 @enum Bird bird = (Bird) 1;@ 86 93 \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.