Changeset 503c350
- Timestamp:
- Aug 4, 2024, 12:22:17 PM (3 months ago)
- Branches:
- master
- Children:
- a57ad8a
- Parents:
- 1e12f07
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
doc/theses/jiada_liang_MMath/background.tex
r1e12f07 r503c350 235 235 \section{\CFA} 236 236 237 \CFA in \emph{not} an object-oriented programming-language, \ie functions cannot be nested in aggregate types, and hence, there is no receive notation for calling functions, \eg @obj.method(...)@, where the first argument proceeds the call. 238 The following section provide short descriptions of \CFA features mentioned further in the thesis. 237 \CFA in \emph{not} an object-oriented programming-language, \ie functions cannot be nested in aggregate types, and hence, there is no \newterm{receiver} notation for calling functions, \eg @obj.method(...)@, where the first argument proceeds the call and becomes an implicit first (\lstinline[language=C++]{this}) parameter. 238 The following sections provide short descriptions of \CFA features needed further in the thesis. 239 Other \CFA features are presented in-situ with short explanations, or no explanation because the feature is obvious to C programmers. 240 241 242 \subsection{Overloading} 243 244 Overloading allows programmers to use the most meaningful names without fear of name clashes within a program or from external sources, like include files. 245 \begin{quote} 246 There are only two hard things in Computer Science: cache invalidation and naming things. --- Phil Karlton 247 \end{quote} 248 Experience from \CC and \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. 249 In many cases, a programmer has no idea there are name clashes, as they are silently resolved, simplifying the development process. 250 Depending on the language, ambiguous cases are resolved using some form of qualification or casting. 239 251 240 252 … … 268 280 Here, there is a perfect match for the call, @f( 'A' )@ with the number and parameter type of function (2). 269 281 270 Ada, Scala, and \CFA type-systems also use the return type in resolving a call .282 Ada, Scala, and \CFA type-systems also use the return type in resolving a call, to pinpoint the best overloaded name. 271 283 \begin{cfa} 272 284 int f( void ); $\C[1.75in]{// (4); overloaded on return type}$ … … 291 303 \end{cfa} 292 304 The \CFA type system simply treats overloaded variables as an overloaded function returning a value with no parameters. 305 Hence, no significant effort is required to support this feature. 293 306 294 307 … … 303 316 304 317 The prototype for the constructor/destructor are @void ?{}( T &, ... )@ and @void ^?{}( T &, ... )@, respectively. 305 The first parameter is logically, the \lstinline[language=C++]{this} or \lstinline[language=java]{self} in other object-oriented languages, and implicitly passed. 318 The first parameter is logically, the \lstinline[language=C++]{this} or \lstinline[language=Python]{self} in other object-oriented languages, and implicitly passed. 319 \VRef[Figure]{f:CFAConstructorDestructor} shows an example of creating and using a constructor and destructor. 320 Both constructor and destructor can be explicitly called to reuse a variable. 321 322 \begin{figure} 306 323 \begin{cfa} 307 324 struct Employee { … … 309 326 double salary; 310 327 }; 311 void @?{}@( Employee & this, char * name, double salary ) {312 this.name = aalloc( sizeof(name) );313 strcpy( this.name,name );314 this.salary =salary;315 } 316 void @^?{}@( Employee & this) {317 free( this.name );328 void @?{}@( Employee & emp, char * nname, double nsalary ) with( emp ) { // auto qualification 329 name = aalloc( sizeof(nname) ); 330 strcpy( name, nname ); 331 salary = nsalary; 332 } 333 void @^?{}@( Employee & emp ) { 334 free( emp.name ); 318 335 } 319 336 { 320 Employee name = { "Sara Schmidt", 20.5 }; 321 } // implicit destructor call 322 \end{cfa} 323 Both constructor and destructor can be explicitly called. 324 \begin{cfa} 325 Employee name = { "Sara Schmidt", 20.5 }; 326 ... // use name 327 ^?{}( name ); // de-initialize 328 ?{}( name, "Jack Smith", 10.5 }; // re-initialize 329 ... // use name 330 \end{cfa} 337 Employee emp = { "Sara Schmidt", 20.5 }; $\C{// initialize with implicit constructor call}$ 338 ... // use emp 339 ^?{}( emp ); $\C{// explicit de-initialize}$ 340 ?{}( emp, "Jack Smith", 10.5 ); $\C{// explicit re-initialize}$ 341 ... // use emp 342 } $\C{// de-initialize with implicit destructor call}$ 343 \end{cfa} 344 \caption{\CFA Constructor and Destructor} 345 \label{f:CFAConstructorDestructor} 346 \end{figure} 331 347 332 348
Note: See TracChangeset
for help on using the changeset viewer.