source: doc/theses/fangren_yu_MMath/intro.tex @ b965774

Last change on this file since b965774 was 7f2e87a, checked in by Peter A. Buhr <pabuhr@…>, 2 months ago

first proofread of chapter 1

  • Property mode set to 100644
File size: 3.8 KB
Line 
1\chapter{Introduction}
2
3This thesis is exploratory work I did to understand, fix, and extend the \CFA type-system, specifically, the type resolver used to select polymorphic types among overloaded names.
4The \CFA type-system has a number of unique features making it different from all other programming languages.
5
6Overloading allows programmers to use the most meaningful names without fear of name clashes within a program or from external sources, like include files.
7\begin{quote}
8There are only two hard things in Computer Science: cache invalidation and naming things. --- Phil Karlton
9\end{quote}
10Experience 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.
11In many cases, a programmer has no idea there are name clashes, as they are silently resolved, simplifying the development process.
12Depending on the language, ambiguous cases are resolved using some form of qualification and/or casting.
13
14One of the key goals in \CFA is to push the boundary on overloading, and hence, overload resolution.
15
16
17\subsection{Operator Overloading}
18
19Virtually all programming languages overload the arithmetic operators across the basic types using the number and type of parameters and returns.
20Like \CC, \CFA also allows these operators to be overloaded with user-defined types.
21The syntax for operator names uses the @'?'@ character to denote a parameter, \eg prefix and infix increment operators: @?++@, @++?@, and @?+?@.
22\begin{cfa}
23struct S { int i, j };
24S @?+?@( S op1, S op2 ) { return (S){ op1.i + op2.i, op1.j + op2.j }; }
25S s1, s2;
26s1 = s1 @+@ s2;                 $\C[1.75in]{// infix call}$
27s1 = @?+?@( s1, s2 );   $\C{// direct call}\CRT$
28\end{cfa}
29The type system examines each call size and selects the best matching overloaded function based on the number and types of the arguments.
30If there are mixed-mode operands, @2 + 3.5@, the type system, like in C/\CC, attempts (safe) conversions, converting the argument type(s) to the parameter type(s).
31
32
33\subsection{Function Overloading}
34
35Both \CFA and \CC allow function names to be overloaded, as long as their prototypes differ in the number and type of parameters and returns.
36\begin{cfa}
37void f( void );                 $\C[1.75in]{// (1): no parameter}$
38void f( char );                 $\C{// (2): overloaded on the number and parameter type}$
39void f( int, int );             $\C{// (3): overloaded on the number and parameter type}$
40f( 'A' );                               $\C{// select (2)}\CRT$
41\end{cfa}
42In this case, the name @f@ is overloaded depending on the number and parameter types.
43The type system examines each call size and selects the best match based on the number and types of the arguments.
44Here, there is a perfect match for the call, @f( 'A' )@ with the number and parameter type of function (2).
45
46Ada, Scala, and \CFA type-systems also use the return type in resolving a call, to pinpoint the best overloaded name.
47\begin{cfa}
48int f( void );                  $\C[1.75in]{// (4); overloaded on return type}$
49double f( void );               $\C{// (5); overloaded on return type}$
50int i = f();                    $\C{// select (4)}$
51double d = f();                 $\C{// select (5)}\CRT$
52\end{cfa}
53
54
55\subsection{Variable Overloading}
56Unlike almost all programming languages, \CFA has variable overloading within a scope, along with shadow overloading in nested scopes.
57\begin{cfa}
58void foo( double d );
59int v;                              $\C[1.75in]{// (1)}$
60double v;                               $\C{// (2) variable overloading}$
61foo( v );                               $\C{// select (2)}$
62{
63        int v;                          $\C{// (3) shadow overloading}$
64        double v;                       $\C{// (4) and variable overloading}$
65        foo( v );                       $\C{// select (4)}\CRT$
66}
67\end{cfa}
68The \CFA type system simply treats overloaded variables as an overloaded function returning a value with no parameters.
69Hence, no significant effort is required to support this feature.
Note: See TracBrowser for help on using the repository browser.