Ignore:
Timestamp:
Aug 4, 2024, 2:35:13 PM (2 days ago)
Author:
JiadaL <j82liang@…>
Branches:
master
Children:
2514d3d7, 5f210c0
Parents:
b59c21a (diff), 748877f (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge branch 'master' of plg.uwaterloo.ca:software/cfa/cfa-cc

File:
1 edited

Legend:

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

    rb59c21a r7ab24fef  
    240240\section{\CFA}
    241241
    242 \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.
    243 The following section provide short descriptions of \CFA features mentioned further in the thesis.
     242\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.
     243The following sections provide short descriptions of \CFA features needed further in the thesis.
     244Other \CFA features are presented in-situ with short explanations, or no explanation because the feature is obvious to C programmers.
     245
     246
     247\subsection{Overloading}
     248
     249Overloading allows programmers to use the most meaningful names without fear of name clashes within a program or from external sources, like include files.
     250\begin{quote}
     251There are only two hard things in Computer Science: cache invalidation and naming things. --- Phil Karlton
     252\end{quote}
     253Experience 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.
     254In many cases, a programmer has no idea there are name clashes, as they are silently resolved, simplifying the development process.
     255Depending on the language, ambiguous cases are resolved using some form of qualification or casting.
    244256
    245257
     
    273285Here, there is a perfect match for the call, @f( 'A' )@ with the number and parameter type of function (2).
    274286
    275 Ada, Scala, and \CFA type-systems also use the return type in resolving a call.
     287Ada, Scala, and \CFA type-systems also use the return type in resolving a call, to pinpoint the best overloaded name.
    276288\begin{cfa}
    277289int f( void );                  $\C[1.75in]{// (4); overloaded on return type}$
     
    296308\end{cfa}
    297309The \CFA type system simply treats overloaded variables as an overloaded function returning a value with no parameters.
     310Hence, no significant effort is required to support this feature.
    298311
    299312
     
    308321
    309322The prototype for the constructor/destructor are @void ?{}( T &, ... )@ and @void ^?{}( T &, ... )@, respectively.
    310 The first parameter is logically, the \lstinline[language=C++]{this} or \lstinline[language=java]{self} in other object-oriented languages, and implicitly passed.
     323The first parameter is logically, the \lstinline[language=C++]{this} or \lstinline[language=Python]{self} in other object-oriented languages, and implicitly passed.
     324\VRef[Figure]{f:CFAConstructorDestructor} shows an example of creating and using a constructor and destructor.
     325Both constructor and destructor can be explicitly called to reuse a variable.
     326
     327\begin{figure}
    311328\begin{cfa}
    312329struct Employee {
     
    314331        double salary;
    315332};
    316 void @?{}@( Employee & this, char * name, double salary ) {
    317         this.name = aalloc( sizeof(name) );
    318         strcpy( this.name, name );
    319         this.salary = salary;
    320 }
    321 void @^?{}@( Employee & this ) {
    322         free( this.name );
     333void @?{}@( Employee & emp, char * nname, double nsalary ) with( emp ) { // auto qualification
     334        name = aalloc( sizeof(nname) );
     335        strcpy( name, nname );
     336        salary = nsalary;
     337}
     338void @^?{}@( Employee & emp ) {
     339        free( emp.name );
    323340}
    324341{
    325         Employee name = { "Sara Schmidt", 20.5 };
    326 } // implicit destructor call
    327 \end{cfa}
    328 Both constructor and destructor can be explicitly called.
    329 \begin{cfa}
    330         Employee name = { "Sara Schmidt", 20.5 };
    331         ... // use name
    332         ^?{}( name ); // de-initialize
    333         ?{}( name, "Jack Smith", 10.5 }; // re-initialize
    334         ... // use name
    335 \end{cfa}
     342        Employee emp = { "Sara Schmidt", 20.5 }; $\C{// initialize with implicit constructor call}$
     343        ... // use emp
     344        ^?{}( emp ); $\C{// explicit de-initialize}$
     345        ?{}( emp, "Jack Smith", 10.5 ); $\C{// explicit re-initialize}$
     346        ... // use emp
     347} $\C{// de-initialize with implicit destructor call}$
     348\end{cfa}
     349\caption{\CFA Constructor and Destructor}
     350\label{f:CFAConstructorDestructor}
     351\end{figure}
    336352
    337353
Note: See TracChangeset for help on using the changeset viewer.