Changeset 41c25b8 for doc/generic_types
- Timestamp:
- Mar 22, 2017, 9:36:48 PM (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:
- 89e1b16
- Parents:
- 1674ff8
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
doc/generic_types/generic_types.tex
r1674ff8 r41c25b8 139 139 \begin{lstlisting} 140 140 forall(otype T) 141 T identity(T x) { is_141 T identity(T x) { 142 142 return x; 143 143 } … … 294 294 Since @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. 295 295 296 \TODO{} The second is zero-cost ``tag'' structs. 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. 297 316 298 317 \section{Tuples}
Note: See TracChangeset
for help on using the changeset viewer.