# Changeset c13b2b8 for doc/generic_types/generic_types.tex

Ignore:
Timestamp:
Apr 17, 2017, 8:56:25 PM (5 years ago)
Branches:
aaron-thesis, arm-eh, cleanup-dtors, deferred_resn, demangler, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, resolv-new, with_gc
Children:
2ca35b1
Parents:
b31fbf2
Message:

condensed to 22 pages

File:
1 edited

### Legend:

Unmodified
 rb31fbf2 Since bare polymorphic-types provide a restricted set of available operations, \CFA provides a \emph{type assertion}~\cite[pp.~37-44]{Alphard} mechanism to provide further type information, where type assertions may be variable or function declarations that depend on a polymorphic type-variable. For example, the function @twice@ can be defined using the \CFA syntax for operator overloading: \newpage \begin{lstlisting} forall( otype T | { T ?+?(T, T); } ) T twice( T x ) { return x + x; } $\C{// ? denotes operands}$ int comp( const void * t1, const void * t2 ) { return *(double *)t1 < *(double *)t2 ? -1 : *(double *)t2 < *(double *)t1 ? 1 : 0; } double vals[10] = { /* 10 floating-point values */ }; double key = 5.0; double key = 5.0, vals[10] = { /* 10 floating-point values */ }; double * val = (double *)bsearch( &key, vals, 10, sizeof(vals[0]), comp );      $\C{// search sorted array}$ \end{lstlisting} %\begin{tabular}{@{}l@{\hspace{1.5\parindent}}||@{\hspace{1.5\parindent}}l@{}} \begin{lstlisting} short int MAX = ...; int MAX = ...; double MAX = ...; short int s = MAX;  $\C{// select correct MAX}$ int i = MAX; double d = MAX; short int MAX = ...;   int MAX = ...;  double MAX = ...; short int s = MAX;    int i = MAX;    double d = MAX;   $\C{// select correct MAX}$ \end{lstlisting} %\end{lstlisting} This example showcases a variadic-template-like decomposition of the provided argument list. The individual @print@ functions allow printing a single element of a type. The polymorphic @print@ allows printing any list of types, as long as each individual type has a @print@ function. The individual print functions can be used to build up more complicated @print@ functions, such as for @S@, which is something that cannot be done with @printf@ in C. The polymorphic @print@ allows printing any list of types, where as each individual type has a @print@ function. The individual print functions can be used to build up more complicated @print@ functions, such as @S@, which cannot be done with @printf@ in C. Finally, it is possible to use @ttype@ polymorphism to provide arbitrary argument forwarding functions. In addition, there are interesting future directions for the polymorphism design. Notably, \CC template functions trade compile time and code bloat for optimal runtime of individual instantiations of polymorphic functions. \CFA polymorphic functions, by contrast, uses a dynamic virtual dispatch. \CFA polymorphic functions uses a dynamic virtual-dispatch. The runtime overhead of this approach is low, but not as low as inlining, and it may be beneficial to provide a mechanism for performance-sensitive code. Two promising approaches are an @inline@ annotation at polymorphic function call sites to create a template-specialization of the function (provided the code is visible) or placing an @inline@ annotation on polymorphic function-definitions to instantiate a specialized version for some set of types.