Ignore:
Timestamp:
Aug 4, 2024, 12:22:17 PM (2 days ago)
Author:
Peter A. Buhr <pabuhr@…>
Branches:
master
Children:
a57ad8a
Parents:
1e12f07
Message:

move overload material into background chapter

File:
1 edited

Legend:

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

    r1e12f07 r503c350  
    235235\section{\CFA}
    236236
    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.
     238The following sections provide short descriptions of \CFA features needed further in the thesis.
     239Other \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
     244Overloading 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}
     246There are only two hard things in Computer Science: cache invalidation and naming things. --- Phil Karlton
     247\end{quote}
     248Experience 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.
     249In many cases, a programmer has no idea there are name clashes, as they are silently resolved, simplifying the development process.
     250Depending on the language, ambiguous cases are resolved using some form of qualification or casting.
    239251
    240252
     
    268280Here, there is a perfect match for the call, @f( 'A' )@ with the number and parameter type of function (2).
    269281
    270 Ada, Scala, and \CFA type-systems also use the return type in resolving a call.
     282Ada, Scala, and \CFA type-systems also use the return type in resolving a call, to pinpoint the best overloaded name.
    271283\begin{cfa}
    272284int f( void );                  $\C[1.75in]{// (4); overloaded on return type}$
     
    291303\end{cfa}
    292304The \CFA type system simply treats overloaded variables as an overloaded function returning a value with no parameters.
     305Hence, no significant effort is required to support this feature.
    293306
    294307
     
    303316
    304317The 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.
     318The 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.
     320Both constructor and destructor can be explicitly called to reuse a variable.
     321
     322\begin{figure}
    306323\begin{cfa}
    307324struct Employee {
     
    309326        double salary;
    310327};
    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 );
     328void @?{}@( Employee & emp, char * nname, double nsalary ) with( emp ) { // auto qualification
     329        name = aalloc( sizeof(nname) );
     330        strcpy( name, nname );
     331        salary = nsalary;
     332}
     333void @^?{}@( Employee & emp ) {
     334        free( emp.name );
    318335}
    319336{
    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}
    331347
    332348
Note: See TracChangeset for help on using the changeset viewer.