- Timestamp:
- Apr 3, 2017, 9:01:28 AM (8 years ago)
- Branches:
- ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, deferred_resn, demangler, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, with_gc
- Children:
- 36e05a2
- Parents:
- 53f9448
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
doc/generic_types/generic_types.tex
r53f9448 rea73caf 51 51 %mathescape=true, % LaTeX math escape in CFA code $...$ 52 52 escapechar=\$, % LaTeX escape in CFA code 53 %keepspaces=true, %53 keepspaces=true, % 54 54 showstringspaces=false, % do not show spaces with cup 55 55 showlines=true, % show blank lines at end of code … … 167 167 \begin{lstlisting} 168 168 forall( otype T `| { int ?<?( T, T ); }` ) $\C{// type assertion}$ 169 void qsort( const T * arr, size_t dim ension);169 void qsort( const T * arr, size_t dim ); 170 170 forall( otype T `| { int ?<?( T, T ); }` ) $\C{// type assertion}$ 171 T * bsearch( T key, const T * arr, size_t dim ension);171 T * bsearch( T key, const T * arr, size_t dim ); 172 172 double vals[10] = { /* 10 floating-point values */ }; 173 173 qsort( vals, 10 ); $\C{// sort array}$ … … 179 179 Crucial to the design of a new programming language are the libraries to access thousands of external features. 180 180 \CFA inherits a massive compatible library-base, where other programming languages have to rewrite or provide fragile inter-language communication with C. 181 A simple example is leveraging the existing type-unsafe C @bsearch@:181 A simple example is leveraging the existing type-unsafe (@void *@) C @bsearch@, shown here searching a floating-point array: 182 182 \begin{lstlisting} 183 183 void * bsearch( const void * key, const void * base, size_t nmemb, size_t size, 184 184 int (* compar)(const void *, const void *)); 185 185 int comp( const void * t1, const void * t2 ) { return *(double *)t1 < *(double *)t2 ? -1 : 186 187 double key = ...;188 double * v = (double *)bsearch( &key, vals, size, sizeof( vals[0]), comp );186 *(double *)t2 < *(double *)t1 ? 1 : 0; } 187 double key = 5.0; 188 double * val = (double *)bsearch( &key, vals, size, sizeof(vals[0]), comp ); 189 189 \end{lstlisting} 190 190 but providing a type-safe \CFA overloaded wrapper. 191 191 \begin{lstlisting} 192 forall( otype T | { int ?<?( T, T ); } ) 193 T * bsearch( T key, const T * arr, size_t dimension ) { 192 forall( otype T | { int ?<?( T, T ); } ) T * bsearch( T key, const T * arr, size_t dim ) { 194 193 int comp( const void * t1, const void * t2 ) { /* as above with double changed to T */ } 195 return (T *)bsearch( &key, arr, dimension, sizeof(T), comp ); 196 } 197 forall( otype T | { int ?<?( T, T ); } ) 198 unsigned int bsearch( T key, const T * arr, size_t dimension ) { 199 T *result = bsearch( key, arr, dimension ); $\C{// call first version}$ 200 return result ? result - arr : dimension; $\C{// pointer subtraction includes sizeof(T)}$ 201 } 202 double * val = bsearch( 5.0, vals, 10 ); $\C{// selection based on return type}$ 194 return (T *)bsearch( &key, arr, dim, sizeof(T), comp ); 195 } 196 forall( otype T | { int ?<?( T, T ); } ) unsigned int bsearch( T key, const T * arr, size_t dim ) { 197 T *result = bsearch( key, arr, dim ); $\C{// call first version}$ 198 return result ? result - arr : dim; $\C{// pointer subtraction includes sizeof(T)}$ 199 } 200 double * val = bsearch( 5.0, vals, 10 ); $\C{// selection based on return type}$ 203 201 int posn = bsearch( 5.0, vals, 10 ); 204 202 \end{lstlisting} … … 209 207 Call-site inferencing and nested functions provide a localized form of inheritance. For example, @qsort@ only sorts in ascending order using @<@. However, it is trivial to locally change this behaviour: 210 208 \begin{lstlisting} 211 { 212 int ?<?( double x, double y ) { return x `>` y; } $\C{// override behaviour}$ 209 { int ?<?( double x, double y ) { return x `>` y; } $\C{// override behaviour}$ 213 210 qsort( vals, size ); $\C{// descending sort}$ 214 211 } … … 217 214 Hence, programmers can easily form new local environments to maximize reuse of existing functions and types. 218 215 219 Finally, variables can also be overloaded: 220 216 Finally, variables may be overloaded: 217 \lstDeleteShortInline@ 218 \par\smallskip 219 \begin{tabular}{@{}l@{\hspace{\parindent}}|@{\hspace{\parindent}}l@{}} 221 220 \begin{lstlisting} 222 221 short int MAX = ...; … … 224 223 double MAX = ...; 225 224 \end{lstlisting} 225 & 226 226 \begin{lstlisting} 227 227 short int s = MAX; // select correct MAX … … 229 229 double d = MAX; 230 230 \end{lstlisting} 231 231 \end{tabular} 232 \lstMakeShortInline@ 233 \smallskip\par\noindent 234 Hence, the single name @MAX@ replaces all the C type-specific names: @SHRT_MAX@, @INT_MAX@, @DBL_MAX@. 232 235 233 236 \subsection{Traits} … … 259 262 }; 260 263 forall( otype T | summable( T ) ) 261 T sum( T a[$\,$], size_t dim ension) {264 T sum( T a[$\,$], size_t dim ) { 262 265 T total = { 0 }; $\C{// instantiate T from 0}$ 263 for ( unsigned int i = 0; i < dim ension; i += 1 )266 for ( unsigned int i = 0; i < dim; i += 1 ) 264 267 total += a[i]; $\C{// select appropriate +}$ 265 268 return total;
Note: See TracChangeset
for help on using the changeset viewer.