Changes in doc/rob_thesis/intro.tex [0eb18557:f92aa32]
- File:
-
- 1 edited
-
doc/rob_thesis/intro.tex (modified) (21 diffs)
Legend:
- Unmodified
- Added
- Removed
-
doc/rob_thesis/intro.tex
r0eb18557 rf92aa32 16 16 Therefore, these design principles must be kept in mind throughout the design and development of new language features. 17 17 In order to appeal to existing C programmers, great care must be taken to ensure that new features naturally feel like C. 18 These goals ensure existing C code-bases can be converted to \CFA incrementally with minimal effort, and C programmers can productively generate \CFA code without training beyond the features being used.19 Unfortunately, \CC is actively diverging from C, so incremental additions require significant effort and training, coupled with multiple legacy design-choices that cannot be updated.20 21 18 The remainder of this section describes some of the important new features that currently exist in \CFA, to give the reader the necessary context in which the new features presented in this thesis must dovetail. 22 19 … … 56 53 \end{cfacode} 57 54 Compound literals create an unnamed object, and result in an lvalue, so it is legal to assign a value into a compound literal or to take its address \cite[p.~86]{C11}. 58 Syntactically, compound literals look like a cast operator followed by a brace-enclosed initializer, but semantically are different from a C cast, which only applies basic conversions and coercions andis never an lvalue.55 Syntactically, compound literals look like a cast operator followed by a brace-enclosed initializer, but semantically are different from a C cast, which only applies basic conversions and is never an lvalue. 59 56 60 57 \subsection{Overloading} … … 62 59 Overloading is the ability to specify multiple entities with the same name. 63 60 The most common form of overloading is function overloading, wherein multiple functions can be defined with the same name, but with different signatures. 64 C provides a small amount of built-in overloading, \eg + is overloaded for the basic types. 65 Like in \CC, \CFA allows user-defined overloading based both on the number of parameters and on the types of parameters. 61 Like in \CC, \CFA allows overloading based both on the number of parameters and on the types of parameters. 66 62 \begin{cfacode} 67 63 void f(void); // (1) … … 96 92 There are times when a function should logically return multiple values. 97 93 Since a function in standard C can only return a single value, a programmer must either take in additional return values by address, or the function's designer must create a wrapper structure to package multiple return-values. 98 For example, the first approach:99 94 \begin{cfacode} 100 95 int f(int * ret) { // returns a value through parameter ret … … 106 101 int res1 = g(&res2); // explicitly pass storage 107 102 \end{cfacode} 108 is awkward because it requires the caller to explicitly allocate memory for $n$ result variables, even if they are only temporary values used as a subexpression, or even not used at all.109 The secondapproach:103 The former solution is awkward because it requires the caller to explicitly allocate memory for $n$ result variables, even if they are only temporary values used as a subexpression, or even not used at all. 104 The latter approach: 110 105 \begin{cfacode} 111 106 struct A { … … 118 113 ... res3.x ... res3.y ... // use result values 119 114 \end{cfacode} 120 is awkward because the caller hasto either learn the field names of the structure or learn the names of helper routines to access the individual return values.121 Both approaches are syntactically unnatural.115 requires the caller to either learn the field names of the structure or learn the names of helper routines to access the individual return values. 116 Both solutions are syntactically unnatural. 122 117 123 118 In \CFA, it is possible to directly declare a function returning multiple values. … … 170 165 \begin{cfacode} 171 166 struct A { int i; }; 172 int ?+?(A x, A y); // '?'s represent operands167 int ?+?(A x, A y); 173 168 bool ?<?(A x, A y); 174 169 \end{cfacode} 175 170 Notably, the only difference is syntax. 176 171 Most of the operators supported by \CC for operator overloading are also supported in \CFA. 177 Of notable exception are the logical operators ( \eg @||@), the sequence operator (\ie @,@), and the member-access operators (\eg@.@ and \lstinline{->}).172 Of notable exception are the logical operators (e.g. @||@), the sequence operator (i.e. @,@), and the member-access operators (e.g. @.@ and \lstinline{->}). 178 173 179 174 Finally, \CFA also permits overloading variable identifiers. … … 248 243 template<typename T> 249 244 T sum(T *arr, int n) { 250 T t; // default construct => 0245 T t; 251 246 for (; n > 0; n--) t += arr[n-1]; 252 247 return t; … … 266 261 \end{cfacode} 267 262 The first thing to note here is that immediately following the declaration of @otype T@ is a list of \emph{type assertions} that specify restrictions on acceptable choices of @T@. 268 In particular, the assertions above specify that there must be a n assignment from \zero to @T@ and an addition assignment operator from @T@ to @T@.263 In particular, the assertions above specify that there must be a an assignment from \zero to @T@ and an addition assignment operator from @T@ to @T@. 269 264 The existence of an assignment operator from @T@ to @T@ and the ability to create an object of type @T@ are assumed implicitly by declaring @T@ with the @otype@ type-class. 270 265 In addition to @otype@, there are currently two other type-classes. … … 286 281 A major difference between the approaches of \CC and \CFA to polymorphism is that the set of assumed properties for a type is \emph{explicit} in \CFA. 287 282 One of the major limiting factors of \CC's approach is that templates cannot be separately compiled. 288 In contrast, the explicit nature of assertions allows \CFA's polymorphic functions to be separately compiled, as the function prototype states all necessary requirements separate from the implementation. 289 For example, the prototype for the previous sum function is 290 \begin{cfacode} 291 forall(otype T | **R**{ T ?=?(T *, zero_t); T ?+=?(T *, T); }**R**) 292 T sum(T *arr, int n); 293 \end{cfacode} 294 With this prototype, a caller in another translation unit knows all of the constraints on @T@, and thus knows all of the operations that need to be made available to @sum@. 283 In contrast, the explicit nature of assertions allows \CFA's polymorphic functions to be separately compiled. 295 284 296 285 In \CFA, a set of assertions can be factored into a \emph{trait}. … … 307 296 This capability allows specifying the same set of assertions in multiple locations, without the repetition and likelihood of mistakes that come with manually writing them out for each function declaration. 308 297 309 An interesting application of return-type resolution and polymorphism is a type-safe version of@malloc@.298 An interesting application of return-type resolution and polymorphism is with type-safe @malloc@. 310 299 \begin{cfacode} 311 300 forall(dtype T | sized(T)) … … 327 316 328 317 In object-oriented programming languages, type invariants are typically established in a constructor and maintained throughout the object's lifetime. 329 These assertions are typically achieved through a combination of access -control modifiers and a restricted interface.318 These assertions are typically achieved through a combination of access control modifiers and a restricted interface. 330 319 Typically, data which requires the maintenance of an invariant is hidden from external sources using the \emph{private} modifier, which restricts reads and writes to a select set of trusted routines, including member functions. 331 320 It is these trusted routines that perform all modifications to internal data in a way that is consistent with the invariant, by ensuring that the invariant holds true at the end of the routine call. … … 399 388 In other languages, a hybrid situation exists where resources escape the allocation block, but ownership is precisely controlled by the language. 400 389 This pattern requires a strict interface and protocol for a data structure, consisting of a pre-initialization and a post-termination call, and all intervening access is done via interface routines. 401 This kind of encapsulation is popular in object-oriented programming languages, and like the stack, it takes care of a significant portion of resource -management cases.390 This kind of encapsulation is popular in object-oriented programming languages, and like the stack, it takes care of a significant portion of resource management cases. 402 391 403 392 For example, \CC directly supports this pattern through class types and an idiom known as RAII \footnote{Resource Acquisition is Initialization} by means of constructors and destructors. … … 410 399 In the context of \CFA, a non-trivial constructor is either a user defined constructor or an auto-generated constructor that calls a non-trivial constructor. 411 400 412 For the remaining resource ownership cases, a programmer must follow a brittle, explicit protocol for freeing resources or an implicit protocol enforced bythe programming language.401 For the remaining resource ownership cases, programmer must follow a brittle, explicit protocol for freeing resources or an implicit protocol implemented via the programming language. 413 402 414 403 In garbage collected languages, such as Java, resources are largely managed by the garbage collector. 415 Still, garbage collectors typically focus only on memory management.404 Still, garbage collectors are typically focus only on memory management. 416 405 There are many kinds of resources that the garbage collector does not understand, such as sockets, open files, and database connections. 417 406 In particular, Java supports \emph{finalizers}, which are similar to destructors. 418 Unfortunately, finalizers are only guaranteed to be called before an object is reclaimed by the garbage collector \cite[p.~373]{Java8}, which may not happen if memory use is not contentious.407 Sadly, finalizers are only guaranteed to be called before an object is reclaimed by the garbage collector \cite[p.~373]{Java8}, which may not happen if memory use is not contentious. 419 408 Due to operating-system resource-limits, this is unacceptable for many long running programs. 420 409 Instead, the paradigm in Java requires programmers to manually keep track of all resources \emph{except} memory, leading many novices and experts alike to forget to close files, etc. … … 461 450 \end{javacode} 462 451 Variables declared as part of a try-with-resources statement must conform to the @AutoClosable@ interface, and the compiler implicitly calls @close@ on each of the variables at the end of the block. 463 Depending on when the exception is raised, both @out@ and @log@ are null, @log@ is null, or both are non-null, therefore, the cleanup for these variables at the end is a utomatically guarded and conditionally executed to prevent null-pointer exceptions.452 Depending on when the exception is raised, both @out@ and @log@ are null, @log@ is null, or both are non-null, therefore, the cleanup for these variables at the end is appropriately guarded and conditionally executed to prevent null-pointer exceptions. 464 453 465 454 While Rust \cite{Rust} does not enforce the use of a garbage collector, it does provide a manual memory management environment, with a strict ownership model that automatically frees allocated memory and prevents common memory management errors. … … 497 486 There is no runtime cost imposed on these restrictions, since they are enforced at compile-time. 498 487 499 Rust provides RAII through the @Drop@ trait, allowing arbitrary code to execute when the object goes out of scope, providing automatic clean up of auxiliary resources,much like a \CC program.488 Rust provides RAII through the @Drop@ trait, allowing arbitrary code to execute when the object goes out of scope, allowing Rust programs to automatically clean up auxiliary resources much like a \CC program. 500 489 \begin{rustcode} 501 490 struct S { … … 504 493 505 494 impl Drop for S { // RAII for S 506 fn drop(&mut self) { // destructor495 fn drop(&mut self) { 507 496 println!("dropped {}", self.name); 508 497 } … … 569 558 tuple<int, int, int> triple(10, 20, 30); 570 559 auto & [t1, t2, t3] = triple; 571 t2 = 0; // changes middle element oftriple560 t2 = 0; // changes triple 572 561 573 562 struct S { int x; double y; }; … … 575 564 auto [x, y] = s; // unpack s 576 565 \end{cppcode} 577 Structured bindings allow unpacking any struct urewith all public non-static data members into fresh local variables.566 Structured bindings allow unpacking any struct with all public non-static data members into fresh local variables. 578 567 The use of @&@ allows declaring new variables as references, which is something that cannot be done with @std::tie@, since \CC references do not support rebinding. 579 568 This extension requires the use of @auto@ to infer the types of the new variables, so complicated expressions with a non-obvious type must be documented with some other mechanism. 580 569 Furthermore, structured bindings are not a full replacement for @std::tie@, as it always declares new variables. 581 570 582 Like \CC, D provides tuples through a library variadic -template structure.571 Like \CC, D provides tuples through a library variadic template struct. 583 572 In D, it is possible to name the fields of a tuple type, which creates a distinct type. 584 573 % http://dlang.org/phobos/std_typecons.html … … 611 600 \end{smlcode} 612 601 Here, the function @binco@ appears to take 2 arguments, but it actually takes a single argument which is implicitly decomposed via pattern matching. 613 Tuples are a foundational tool in SML, allowing the creation of arbitrarily -complex structured data-types.602 Tuples are a foundational tool in SML, allowing the creation of arbitrarily complex structured data types. 614 603 615 604 Scala, like \CC, provides tuple types through the standard library \cite{Scala}. … … 664 653 Since the variadic arguments are untyped, it is up to the function to interpret any data that is passed in. 665 654 Additionally, the interface to manipulate @va_list@ objects is essentially limited to advancing to the next argument, without any built-in facility to determine when the last argument is read. 666 This limitationrequires the use of an \emph{argument descriptor} to pass information to the function about the structure of the argument list, including the number of arguments and their types.655 This requires the use of an \emph{argument descriptor} to pass information to the function about the structure of the argument list, including the number of arguments and their types. 667 656 The format string in @printf@ is one such example of an argument descriptor. 668 657 \begin{cfacode}
Note:
See TracChangeset
for help on using the changeset viewer.