Changeset 0723a57 for doc/papers/general
- Timestamp:
- Feb 7, 2018, 5:04:22 PM (7 years ago)
- Branches:
- ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, deferred_resn, demangler, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, with_gc
- Children:
- 3f8ab8f
- Parents:
- 695571c
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
doc/papers/general/Paper.tex
r695571c r0723a57 1052 1052 \label{s:WithClauseStatement} 1053 1053 1054 Grouping heterogenous data into \newterm{aggregate}s is a common programming practice, and an aggregate can be further organized into more complex structures, such as arrays and containers:1054 Grouping heterogenous data into \newterm{aggregate}s (structure/union) is a common programming practice, and an aggregate can be further organized into more complex structures, such as arrays and containers: 1055 1055 \begin{cfa} 1056 struct S { $\C{// aggregate}$1057 char c; $\C{// fields}$1056 struct S { $\C{// aggregate}$ 1057 char c; $\C{// fields}$ 1058 1058 int i; 1059 1059 double d; … … 1061 1061 S s, as[10]; 1062 1062 \end{cfa} 1063 However, routines manipulating aggregates have repeition ofthe aggregate name to access its containing fields:1063 However, routines manipulating aggregates must repeat the aggregate name to access its containing fields: 1064 1064 \begin{cfa} 1065 1065 void f( S s ) { 1066 `s.`c; `s.`i; `s.`d; $\C{// access containing fields}$1066 `s.`c; `s.`i; `s.`d; $\C{// access containing fields}$ 1067 1067 } 1068 1068 \end{cfa} … … 1070 1070 \begin{C++} 1071 1071 class C { 1072 char c; $\C{// fields}$1072 char c; $\C{// fields}$ 1073 1073 int i; 1074 1074 double d; 1075 int mem() { $\C{// implicit "this" parameter}$1076 `this->`c; `this->`i; `this->`d; $\C{// access containing fields}$1075 int mem() { $\C{// implicit "this" parameter}$ 1076 `this->`c; `this->`i; `this->`d; $\C{// access containing fields}$ 1077 1077 } 1078 1078 } 1079 1079 \end{C++} 1080 Nesting of member routines in a \lstinline[language=C++]@class@ allows eliding \lstinline[language=C++]@this->@ because of nested lexical-scoping.1080 Nesting of member routines in a \lstinline[language=C++]@class@ allows eliding \lstinline[language=C++]@this->@ because of lexical scoping. 1081 1081 1082 1082 % In object-oriented programming, there is an implicit first parameter, often names @self@ or @this@, which is elided. … … 1088 1088 % \TODO{Fill out section. Be sure to mention arbitrary expressions in with-blocks, recent change driven by Thierry to prioritize field name over parameters.} 1089 1089 1090 \CFA provides a @with@ clause/statement (see Pascal~\cite[\S~4.F]{Pascal}) to elide aggregate qualification to fields by opening a scope containing field identifiers.1091 Hence, the qualified fields become variables , and making it easier to optimizefield references in a block.1090 \CFA provides a @with@ clause/statement (see Pascal~\cite[\S~4.F]{Pascal}) to elide aggregate qualification to fields by opening a scope containing the field identifiers. 1091 Hence, the qualified fields become variables with the side-effect that it is easier to optimizing field references in a block. 1092 1092 \begin{cfa} 1093 void f( S s ) `with( s )` { $\C{// with clause}$1094 c; i; d; $\C{\color{red}// s.c, s.i, s.d}$1093 void f( S s ) `with( s )` { $\C{// with clause}$ 1094 c; i; d; $\C{\color{red}// s.c, s.i, s.d}$ 1095 1095 } 1096 1096 \end{cfa} … … 1098 1098 \begin{cfa} 1099 1099 int mem( S & this ) `with( this )` { $\C{// with clause}$ 1100 c; i; d; $\C{\color{red}// this.c, this.i, this.d}$1100 c; i; d; $\C{\color{red}// this.c, this.i, this.d}$ 1101 1101 } 1102 1102 \end{cfa} 1103 The key generality over the object-oriented approach is that one aggregate parameter \lstinline[language=C++]@this@ is not treated specially over other aggregate parameters:1103 The generality over the object-oriented approach is that multiple aggregate parameters can be opened, not just \lstinline[language=C++]@this@: 1104 1104 \begin{cfa} 1105 1105 struct T { double m, n; }; 1106 1106 int mem( S & s, T & t ) `with( s, t )` { $\C{// multiple aggregate parameters}$ 1107 c; i; d; $\C{\color{red}// s.c, s.i, s.d}$1108 m; n; $\C{\color{red}// t.m, t.n}$1107 c; i; d; $\C{\color{red}// s.c, s.i, s.d}$ 1108 m; n; $\C{\color{red}// t.m, t.n}$ 1109 1109 } 1110 1110 \end{cfa} 1111 The equivalent object-oriented styleis:1111 The equivalent object-oriented approach is: 1112 1112 \begin{cfa} 1113 int S::mem( T & t ) { $\C{// multiple aggregate parameters}$ 1114 c; i; d; $\C{\color{red}// this-\textgreater.c, this-\textgreater.i, this-\textgreater.d}$ 1115 `t.`m; `t.`n; 1113 int S::mem( T & t ) { $\C{// multiple aggregate parameters}$ 1114 c; i; d; $\C{\color{red}// this-\textgreater.c, this-\textgreater.i, this-\textgreater.d}$ 1115 `t.`m; `t.`n; $\C{// must qualify}$ 1116 } 1117 \end{cfa} 1118 1119 \begin{cfa} 1120 struct S { int i, j; } sv; 1121 with( sv ) { 1122 S & sr = sv; 1123 with( sr ) { 1124 S * sp = &sv; 1125 with( *sp ) { 1126 i = 3; j = 4; $\C{\color{red}// sp-{\textgreater}i, sp-{\textgreater}j}$ 1127 } 1128 i = 3; j = 4; $\C{\color{red}// sr.i, sr.j}$ 1129 } 1130 i = 3; j = 4; $\C{\color{red}// sv.i, sv.j}$ 1116 1131 } 1117 1132 \end{cfa} … … 1122 1137 struct S1 { ... } s1; 1123 1138 struct S2 { ... } s2; 1124 `with( s1 )` { $\C{// with statement}$1139 `with( s1 )` { $\C{// with statement}$ 1125 1140 // access fields of s1 without qualification 1126 `with( s2 )` { $\C{// nesting}$1141 `with( s2 )` { $\C{// nesting}$ 1127 1142 // access fields of s1 and s2 without qualification 1128 1143 } … … 1140 1155 struct T { int i; int k; int m } b, c; 1141 1156 `with( a, b )` { 1142 j + k; $\C{// unambiguous, unique names define unique types}$1143 i; $\C{// ambiguous, same name and type}$1144 a.i + b.i; $\C{// unambiguous, qualification defines unique names}$1145 m; $\C{// ambiguous, same name and no context to define unique type}$1146 m = 5.0; $\C{// unambiguous, same name and context defines unique type}$1147 m = 1; $\C{// unambiguous, same name and context defines unique type}$1148 } 1149 `with( c )` { ... } $\C{// ambiguous, same name and no context}$1150 `with( (S)c )` { ... } $\C{// unambiguous, same name and cast defines unique type}$1157 j + k; $\C{// unambiguous, unique names define unique types}$ 1158 i; $\C{// ambiguous, same name and type}$ 1159 a.i + b.i; $\C{// unambiguous, qualification defines unique names}$ 1160 m; $\C{// ambiguous, same name and no context to define unique type}$ 1161 m = 5.0; $\C{// unambiguous, same name and context defines unique type}$ 1162 m = 1; $\C{// unambiguous, same name and context defines unique type}$ 1163 } 1164 `with( c )` { ... } $\C{// ambiguous, same name and no context}$ 1165 `with( (S)c )` { ... } $\C{// unambiguous, same name and cast defines unique type}$ 1151 1166 \end{cfa} 1152 1167 1153 1168 The components in the "with" clause 1154 1169 1155 with 1170 with( a, b, c ) { ... } 1156 1171 1157 1172 serve 2 purposes: each component provides a type and object. The type must be a
Note: See TracChangeset
for help on using the changeset viewer.