Changeset ffc9f5a for doc/generic_types
- Timestamp:
- Apr 13, 2017, 8:17:28 AM (7 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:
- 5103d7a
- Parents:
- 19518e8
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
doc/generic_types/generic_types.tex
r19518e8 rffc9f5a 342 342 % int is_nominal; $\C{// int now satisfies the nominal trait}$ 343 343 % \end{lstlisting} 344 % 344 % 345 345 % Traits, however, are significantly more powerful than nominal-inheritance interfaces; most notably, traits may be used to declare a relationship \emph{among} multiple types, a property that may be difficult or impossible to represent in nominal-inheritance type systems: 346 346 % \begin{lstlisting} … … 353 353 % }; 354 354 % typedef list *list_iterator; 355 % 355 % 356 356 % lvalue int *?( list_iterator it ) { return it->value; } 357 357 % \end{lstlisting} … … 503 503 In many languages, functions can return at most one value; 504 504 however, many operations have multiple outcomes, some exceptional. 505 Consider C's @div@ and @remquo@ functions, which return the quotient and remainder for a division of integer and floating-point values, respectively. 505 Consider C's @div@ and @remquo@ functions, which return the quotient and remainder for a division of integer and floating-point values, respectively. 506 506 \begin{lstlisting} 507 507 typedef struct { int quo, rem; } div_t; … … 524 524 \end{lstlisting} 525 525 Clearly, this approach is straightforward to understand and use; 526 therefore, why do most programming language not support this obvious feature or provide it awkwardly?526 therefore, why do most programming languages not support this obvious feature or provide it awkwardly? 527 527 The answer is that there are complex consequences that cascade through multiple aspects of the language, especially the type-system. 528 528 This section show these consequences and how \CFA deals with them. … … 532 532 533 533 The addition of multiple-return-value (MRV) functions are useless without a syntax for accepting multiple values at the call-site. 534 The simplest mechanism for capturing the return values is variable assignment, allowing the valu seto be retrieved directly.534 The simplest mechanism for capturing the return values is variable assignment, allowing the values to be retrieved directly. 535 535 As such, \CFA allows assigning multiple values from a function into multiple variables, using a square-bracketed list of lvalue expressions (as above), called a \emph{tuple}. 536 536 537 However, functions also use \emph{composition} (nested calls), with the direct consequence that MRV functions must also support composition to be orthogonal with single-return ing-value (SRV) functions, \eg:537 However, functions also use \emph{composition} (nested calls), with the direct consequence that MRV functions must also support composition to be orthogonal with single-return-value (SRV) functions, \eg: 538 538 \begin{lstlisting} 539 539 printf( "%d %d\n", div( 13, 5 ) ); $\C{// return values seperated into arguments}$ … … 568 568 printf( "%d %d\n", qr ); 569 569 \end{lstlisting} 570 \CFA also supports \emph{tuple indexing} to access single components of a t yple expression:570 \CFA also supports \emph{tuple indexing} to access single components of a tuple expression: 571 571 \begin{lstlisting} 572 572 [int, int] * p = &qr; $\C{// tuple pointer}$ … … 606 606 In the call to @g@, the values @y@ and @10@ are structured into a single argument of type @[int, int]@ to match the parameter type of @g@. 607 607 Finally, in the call to @h@, @x@ is flattened to yield an argument list of length 3, of which the first component of @x@ is passed as the first parameter of @h@, and the second component of @x@ and @y@ are structured into the second argument of type @[int, int]@. 608 The flexible structure of tuples permits a simple and expressive function call syntax to work seamlessly with both SR F and MRF, and with any number of arguments of arbitrarily complex structure.608 The flexible structure of tuples permits a simple and expressive function call syntax to work seamlessly with both SRV and MRV functions, and with any number of arguments of arbitrarily complex structure. 609 609 610 610 … … 734 734 f([5, "hello"]); 735 735 \end{lstlisting} 736 where @[5, "hello"]@ is flattened, giving argument list @5, "hello"@, and @T@ binds to @int@ and @U@ binds to @const char *@.736 where @[5, "hello"]@ is flattened, giving argument list @5, "hello"@, and @T@ binds to @int@ and @U@ binds to @const char@. 737 737 Tuples, however, may contain polymorphic components. 738 738 For example, a plus operator can be written to add two triples together. … … 869 869 T2 field_2; 870 870 }; 871 _tuple3 _(int, double, int) y;871 _tuple3(int, double, int) y; 872 872 } 873 873 \end{lstlisting}
Note: See TracChangeset
for help on using the changeset viewer.