Changeset 41c25b8 for doc


Ignore:
Timestamp:
Mar 22, 2017, 9:36:48 PM (7 years ago)
Author:
Aaron Moss <bruceiv@…>
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:
89e1b16
Parents:
1674ff8
Message:

Added tag structs discussion to generics paper.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • doc/generic_types/generic_types.tex

    r1674ff8 r41c25b8  
    139139\begin{lstlisting}
    140140forall(otype T)
    141 T identity(T x) {is_
     141T identity(T x) {
    142142    return x;
    143143}
     
    294294Since @pair(T*, T*)@ is a concrete type, there are no added implicit parameters to @lexcmp@, so the code generated by \CFA{} will be effectively identical to a version of this written in standard C using @void*@, yet the \CFA{} version will be type-checked to ensure that the fields of both pairs and the arguments to the comparison function match in type.
    295295
    296 \TODO{} The second is zero-cost ``tag'' structs.
     296Another useful pattern enabled by re-used dtype-static type instantiations is zero-cost ``tag'' structs. Sometimes a particular bit of information is only useful for type-checking, and can be omitted at runtime. Tag structs can be used to provide this information to the compiler without further runtime overhead, as in the following example:
     297\begin{lstlisting}
     298forall(dtype Unit) struct scalar { unsigned long value; };
     299
     300struct metres {};
     301struct litres {};
     302
     303forall(dtype U)
     304scalar(U) ?+?(scalar(U) a, scalar(U) b) {
     305        return (scalar(U)){ a.value + b.value };
     306}
     307
     308scalar(metres) half_marathon = { 21093 };
     309scalar(litres) swimming_pool = { 2500000 };
     310
     311scalar(metres) marathon = half_marathon + half_marathon;
     312scalar(litres) two_pools = swimming_pool + swimming_pool;
     313marathon + swimming_pool; // ERRORv -- caught by compiler
     314\end{lstlisting}
     315@scalar@ is a dtype-static type, so all uses of it will use a single struct definition, containing only a single @unsigned long@, and can share the same implementations of common routines like @?+?@ -- these implementations may even be separately compiled, unlike \CC{} template functions. However, the \CFA{} type-checker will ensure that matching types are used by all calls to @?+?@, preventing nonsensical computations like adding the length of a marathon to the volume of an olympic pool.
    297316
    298317\section{Tuples}
Note: See TracChangeset for help on using the changeset viewer.