Changeset fcf3493 for doc/theses/jiada_liang_MMath/CFAenum.tex
- Timestamp:
- Jul 30, 2024, 4:07:54 PM (4 months ago)
- Branches:
- master
- Children:
- 2ff76d25
- Parents:
- dd78dbc
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
doc/theses/jiada_liang_MMath/CFAenum.tex
rdd78dbc rfcf3493 114 114 \subsection{Value Conversion} 115 115 C has an implicit type conversion from an enumerator to its base type @int@. 116 Correspondingly, \CFA has an implicit conversion from a typed enumerator to its base type . The feature that allows Typed enumeration117 seemlyless used116 Correspondingly, \CFA has an implicit conversion from a typed enumerator to its base type, allowing typed enumeration to be seemlyless used as 117 a value of its base type. 118 118 \begin{cfa} 119 119 char currency = Dollar; … … 122 122 \end{cfa} 123 123 124 During the resolution of expression e with \CFA enumeration type, \CFA adds @value(e)@ as an additional candidate with an extra \newterm{value} cost. 125 For expression @char currency = Dollar@, the is no defined conversion from Dollar (\CFA enumeration) type to basic type and the conversion cost is @infinite@, 126 thus the only valid candidate is @value(Dollar)@. 127 128 @Value@ is a new category in \CFA's conversion cost model. It is defined to be a more significant factor than a @unsafe@ but weight less than @poly@. 129 The resultin g conversion cost is a 8-tuple: 124 % During the resolution of expression e with \CFA enumeration type, \CFA adds @value(e)@ as an additional candidate with an extra \newterm{value} cost. 125 % For expression @char currency = Dollar@, the is no defined conversion from Dollar (\CFA enumeration) type to basic type and the conversion cost is @infinite@, 126 % thus the only valid candidate is @value(Dollar)@. 127 The implicit conversion induces a \newterm{value cost}, which is a new category in \CFA's conversion cost model to disambiguate function overloading over for both \CFA enumeration and its base type. 128 \begin{cfa} 129 void baz( char ch ); $\C{// (1)}$ 130 void baz( Currency cu ); $\C{// (2)}$ 131 132 baz( Cent ); 133 \end{cfa} 134 While both baz are applicable to \CFA enumeration, using Cent as a char in @candiate (1)@ comes with a @value@ cost, 135 while @candidate (2)@ has @zero@ cost. \CFA always choose a overloaded candidate implemented for a \CFA enumeration itself over a candidate applies on a base type. 136 137 Value cost is defined to be a more significant factor than an @unsafe@ but weight less than @poly@. 138 With @value@ being an additional category, the current \CFA conversion cost is a 8-tuple: 130 139 @@(unsafe, value, poly, safe, sign, vars, specialization, reference)@@. 131 140 … … 137 146 }; 138 147 139 Month a = Februrary; // (1), with cost (0, 1, 0, 0, 0, 0, 0, 0)140 double a = 5.5; // (2), with cost (1, 0, 0, 0, 0, 0, 0, 0)148 Month a = Februrary; $\C{// (1), with cost (0, 1, 0, 0, 0, 0, 0, 0)}$ 149 double a = 5.5; $\C{// (2), with cost (1, 0, 0, 0, 0, 0, 0, 0)}$ 141 150 142 151 bar(a); … … 148 157 forall(T | @CfaEnum(T)@) void bar(T); 149 158 150 bar(a); // (3), with cost (0, 0, 1, 0, 0, 0, 0, 0)159 bar(a); $\C{// (3), with cost (0, 0, 1, 0, 0, 0, 0, 0)}$ 151 160 \end{cfa} 152 161 % @Value@ is designed to be less significant than @poly@ to allow function being generic over \CFA enumeration (see ~\ref{c:trait}). … … 192 201 \section{Enumeration Inheritance} 193 202 194 \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). 203 \CFA Plan-9 inheritance may be used with \CFA enumerations, where Plan-9 inheritance is containment inheritance with implicit unscoping (like a nested unnamed @struct@/@union@ in C). 204 Inheritance can be nested, and a \CFA enumeration can inline enumerators from more than one \CFA enumeration, forming a tree-like structure. 205 Howver, the uniqueness of enumeration label applies to enumerators from supertypes, meaning an enumeration cannot name enumerator with the same label as its subtype's members, or inherits 206 from multiple enumeration that has overlapping enumerator label. As a consequence, a new type cannot inherits both an enumeration and its supertype, or inherit two enumerations with a 207 common supertype (the diamond problem), since such would unavoidably introduce duplicate enumerator labels. 195 208 196 209 \begin{cfa} … … 200 213 \end{cfa} 201 214 202 Enumeration @Name2@ inherits all the enumerators and their values from enumeration @Names@ by containment, and a @Names@ enumeration is a @subtype@ of enumeration @Name2@. 203 Note, that enumerators must be unique in inheritance but enumerator values may be repeated. 215 % Enumeration @Name2@ inherits all the enumerators and their values from enumeration @Names@ by containment, and a @Names@ enumeration is a @subtype@ of enumeration @Name2@. 216 % Note, that enumerators must be unique in inheritance but enumerator values may be repeated. 217 218 @Names2@ is defined with five enumerators, three of which are from @Name@ through containment, and two are self-declared. 219 @Names3@ inherits all five members from @Names2@ and declare two additional enumerators. 204 220 205 221 % The enumeration type for the inheriting type must be the same as the inherited type; … … 211 227 \end{cfa} 212 228 213 Inlined from \CFA enumeration @O@, new enumeration @N@ copies all enumerators from @O@, including those @O@ obtains through inheritance. Enumerators inherited from @O@ 214 keeps same @label@ and @value@, but @position@ may shift to the right if other enumerators or inline enumeration declared in prior of @inline A@. 229 The enumeration type for the inheriting type must be the same as the inherited type. 230 When an enumeration inherits enumerators from another enumeration, it copies the enumerators' @value@ and @label@, even if the @value@ was auto initialized. However, the @position@ as the underlying 231 representation will be the order of the enumerator in new enumeration. 232 % new enumeration @N@ copies all enumerators from @O@, including those @O@ obtains through inheritance. Enumerators inherited from @O@ 233 % keeps same @label@ and @value@, but @position@ may shift to the right if other enumerators or inline enumeration declared in prior of @inline A@. 234 % hence the enumeration type may be omitted for the inheriting enumeration and it is inferred from the inherited enumeration, as for @Name3@. 235 % When inheriting from integral types, automatic numbering may be used, so the inheritance placement left to right is important. 236 215 237 \begin{cfa} 216 238 enum() Phynchocephalia { Tuatara }; … … 220 242 Snake, for example, has the position 0 in Squamata, but 1 in Lepidosauromorpha as Tuatara inherited from Phynchocephalia is position 0 in Lepidosauromorpha. 221 243 222 A subtype enumeration can be casted, or implicitly converted into its supertype, with a safecost.244 A subtype enumeration can be casted, or implicitly converted into its supertype, with a @safe@ cost. 223 245 \begin{cfa} 224 246 enum Squamata squamata_lizard = Lizard;
Note: See TracChangeset
for help on using the changeset viewer.