Ignore:
Timestamp:
Oct 4, 2017, 3:31:34 PM (5 years ago)
Author:
Thierry Delisle <tdelisle@…>
Branches:
aaron-thesis, arm-eh, 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, resolv-new, with_gc
Children:
b7778c1
Parents:
dcfc4b3
Message:

More work on chapter 2 and 3

File:
1 edited

Legend:

Unmodified
Added
Removed
  • doc/proposals/concurrency/text/cforall.tex

    rdcfc4b3 r3628765  
    100100Note that like \CC, \CFA introduces \code{new} and \code{delete}, which behave like \code{malloc} and \code{free} in addition to constructing and destructing objects, after calling \code{malloc} and before calling \code{free} respectively.
    101101
     102\section{Parametric Polymorphism}
     103Routines in \CFA can also be reused for multiple types. This is done using the \code{forall} clause which gives \CFA it's name. \code{forall} clauses allow seperatly compiled routines to support generic usage over multiple types. For example, the following sum function will work for any type which support construction from 0 and addition :
     104\begin{cfacode}
     105//constraint type, 0 and +
     106forall(otype T | { void ?{}(T *, zero_t); T ?+?(T, T); })
     107T sum(T a[ ], size_t size) {
     108        T total = 0;                            //construct T from 0
     109        for(size_t i = 0; i < size; i++)
     110                total = total + a[i];   //select appropriate +
     111        return total;
     112}
     113
     114S sa[5];
     115int i = sum(sa, 5);                             //use S's 0 construction and +
     116\end{cfacode}
     117
     118Since writing constraints on types can become cumbersome for more constrained functions, \CFA also has the concept of traits. Traits are named collection of constraints which can be used both instead and in addition to regular constraints:
     119\begin{cfacode}
     120trait sumable( otype T ) {
     121        void ?{}(T *, zero_t);          //constructor from 0 literal
     122        T ?+?(T, T);                            //assortment of additions
     123        T ?+=?(T *, T);
     124        T ++?(T *);
     125        T ?++(T *);
     126};
     127forall( otype T | sumable(T) )  //use trait
     128T sum(T a[], size_t size);
     129\end{cfacode}
     130
     131\section{with Clause/Statement}
     132Since \CFA lacks the concept of a receiver, certain functions end-up needing to repeat variable names often, to solve this \CFA offers the \code{with} statement which opens an aggregate scope making its fields directly accessible (like Pascal).
     133\begin{cfacode}
     134struct S { int i, j; };
     135int mem(S & this) with this             //with clause
     136        i = 1;                                          //this->i
     137        j = 2;                                          //this->j
     138}
     139int foo() {
     140        struct S1 { ... } s1;
     141        struct S2 { ... } s2;
     142        with s1                                         //with statement
     143        {
     144                //access fields of s1
     145                //without qualification
     146                with s2                                 //nesting
     147                {
     148                        //access fields of s1 and s2
     149                        //without qualification
     150                }
     151        }
     152        with s1, s2                             //scopes open in parallel
     153        {
     154                //access fields of s1 and s2
     155                //without qualification
     156        }
     157}
     158\end{cfacode}
     159
    102160For more information on \CFA see \cite{cforall-ug,rob-thesis,www-cfa}.
Note: See TracChangeset for help on using the changeset viewer.