Changes in / [89e1b16:1e4f05e]


Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • doc/generic_types/generic_types.tex

    r89e1b16 r1e4f05e  
    139139\begin{lstlisting}
    140140forall(otype T)
    141 T identity(T x) {
     141T identity(T x) {is_
    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 Another 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}
    298 forall(dtype Unit) struct scalar { unsigned long value; };
    299 
    300 struct metres {};
    301 struct litres {};
    302 
    303 forall(dtype U)
    304 scalar(U) ?+?(scalar(U) a, scalar(U) b) {
    305         return (scalar(U)){ a.value + b.value };
    306 }
    307 
    308 scalar(metres) half_marathon = { 21093 };
    309 scalar(litres) swimming_pool = { 2500000 };
    310 
    311 scalar(metres) marathon = half_marathon + half_marathon;
    312 scalar(litres) two_pools = swimming_pool + swimming_pool;
    313 marathon + 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.
     296\TODO{} The second is zero-cost ``tag'' structs.
    316297
    317298\section{Tuples}
Note: See TracChangeset for help on using the changeset viewer.