1 | \chapter{Introduction} |
---|
2 | |
---|
3 | This 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. |
---|
4 | The \CFA type-system has a number of unique features making it different from all other programming languages. |
---|
5 | |
---|
6 | Overloading 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} |
---|
8 | There are only two hard things in Computer Science: cache invalidation and naming things. --- Phil Karlton |
---|
9 | \end{quote} |
---|
10 | 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. |
---|
11 | In many cases, a programmer has no idea there are name clashes, as they are silently resolved, simplifying the development process. |
---|
12 | Depending on the language, ambiguous cases are resolved using some form of qualification and/or casting. |
---|
13 | |
---|
14 | One of the key goals in \CFA is to push the boundary on overloading, and hence, overload resolution. |
---|
15 | |
---|
16 | |
---|
17 | \subsection{Operator Overloading} |
---|
18 | |
---|
19 | Virtually all programming languages overload the arithmetic operators across the basic types using the number and type of parameters and returns. |
---|
20 | Like \CC, \CFA also allows these operators to be overloaded with user-defined types. |
---|
21 | The syntax for operator names uses the @'?'@ character to denote a parameter, \eg prefix and infix increment operators: @?++@, @++?@, and @?+?@. |
---|
22 | \begin{cfa} |
---|
23 | struct S { int i, j }; |
---|
24 | S @?+?@( S op1, S op2 ) { return (S){ op1.i + op2.i, op1.j + op2.j }; } |
---|
25 | S s1, s2; |
---|
26 | s1 = s1 @+@ s2; $\C[1.75in]{// infix call}$ |
---|
27 | s1 = @?+?@( s1, s2 ); $\C{// direct call}\CRT$ |
---|
28 | \end{cfa} |
---|
29 | The type system examines each call size and selects the best matching overloaded function based on the number and types of the arguments. |
---|
30 | If 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 | |
---|
35 | Both \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} |
---|
37 | void f( void ); $\C[1.75in]{// (1): no parameter}$ |
---|
38 | void f( char ); $\C{// (2): overloaded on the number and parameter type}$ |
---|
39 | void f( int, int ); $\C{// (3): overloaded on the number and parameter type}$ |
---|
40 | f( 'A' ); $\C{// select (2)}\CRT$ |
---|
41 | \end{cfa} |
---|
42 | In this case, the name @f@ is overloaded depending on the number and parameter types. |
---|
43 | The type system examines each call size and selects the best match based on the number and types of the arguments. |
---|
44 | Here, there is a perfect match for the call, @f( 'A' )@ with the number and parameter type of function (2). |
---|
45 | |
---|
46 | Ada, Scala, and \CFA type-systems also use the return type in resolving a call, to pinpoint the best overloaded name. |
---|
47 | \begin{cfa} |
---|
48 | int f( void ); $\C[1.75in]{// (4); overloaded on return type}$ |
---|
49 | double f( void ); $\C{// (5); overloaded on return type}$ |
---|
50 | int i = f(); $\C{// select (4)}$ |
---|
51 | double d = f(); $\C{// select (5)}\CRT$ |
---|
52 | \end{cfa} |
---|
53 | |
---|
54 | |
---|
55 | \subsection{Variable Overloading} |
---|
56 | Unlike almost all programming languages, \CFA has variable overloading within a scope, along with shadow overloading in nested scopes. |
---|
57 | \begin{cfa} |
---|
58 | void foo( double d ); |
---|
59 | int v; $\C[1.75in]{// (1)}$ |
---|
60 | double v; $\C{// (2) variable overloading}$ |
---|
61 | foo( 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} |
---|
68 | The \CFA type system simply treats overloaded variables as an overloaded function returning a value with no parameters. |
---|
69 | Hence, no significant effort is required to support this feature. |
---|