This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
at:tutorial:modular [2007/04/24 15:43] tvcutsem * |
at:tutorial:modular [2013/05/17 20:24] tvcutsem adjusted |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== Modular Programming ====== | ||
- | < | ||
- | This Tutorial is still under heavy construction! | ||
- | </ | ||
- | |||
- | In this tutorial chapter, we introduce AmbientTalk' | ||
- | |||
- | ===== Namespaces and the lobby ===== | ||
- | |||
- | In AmbientTalk, | ||
- | |||
- | ==== Namespaces ==== | ||
- | In order for AmbientTalk programs to use code defined in other files, a file loading mechanism is required. AmbientTalk provides the concept of a namespace object to perform this task. A namespace object is an object which is internally connected to a directory path on the local file system (how a namespace object is tied to this path will be explained later). By accessing a namespace object' | ||
- | |||
- | < | ||
- | object: { | ||
- | def factorial(n) { | ||
- | if: (n = 0) then: { | ||
- | 1 | ||
- | } else: { | ||
- | n*factorial(n-1) | ||
- | } | ||
- | }; | ||
- | def fib(n) { | ||
- | if: (n <= 1) then: { | ||
- | n | ||
- | } else: { | ||
- | fib(n-1)+fib(n-2) | ||
- | } | ||
- | }; | ||
- | }; | ||
- | </ | ||
- | |||
- | This file can now be loaded by means of the '' | ||
- | < | ||
- | def mathModule := lib.math; | ||
- | system.println(mathModule.factorial(5)); | ||
- | </ | ||
- | |||
- | When the namespace object' | ||
- | * a file named '' | ||
- | * a subdirectory named '' | ||
- | In this example, the file '' | ||
- | |||
- | If the '' | ||
- | |||
- | < | ||
- | By representing hierarchical directories as nested namespace objects, the AmbientTalk programmer can abstract over the actual files and directories of the underlying file system. For example, imagine that the designer of the '' | ||
- | |||
- | Obviously, this approach works both ways: if the library designer had started out organizing his project using subdirectories and multiple files, he can always restructure the library' | ||
- | </ | ||
- | |||
- | ==== The lobby ==== | ||
- | |||
- | We have yet to explain how namespaces are initially tied to directories. One possibility is to define a '' | ||
- | < | ||
- | root.home.ambienttalkuser.examples.math | ||
- | </ | ||
- | |||
- | The downside of such as scheme is that the absolute path name of the '' | ||
- | |||
- | The [[at: | ||
- | |||
- | < | ||
- | iat -o lib=/ | ||
- | </ | ||
- | |||
- | Whenever a new actor is created by the AmbientTalk interpreter, | ||
- | |||
- | < | ||
- | AmbientTalk provides an alias for the '' | ||
- | </ | ||
- | |||
- | ==== The current namespace ==== | ||
- | |||
- | AmbientTalk provides a slot named '' | ||
- | |||
- | < | ||
- | def discreteMathModule := ~.discretemath; | ||
- | </ | ||
- | |||
- | Loading a module this way is useful because the author of '' | ||
- | |||
- | ===== Importing objects ===== | ||
- | |||
- | < | ||
- | Warning: importing from namespaces | ||
- | </ | ||
- | |||
- | ===== Objects as traits ===== | ||
- | |||
- | explain: using import as an object-composition mechanism: concepts behind traits (required/ | ||
- | |||
- | ===== Classifying objects using stripes ===== | ||
- | |||
- | explain: what are stripes? what kind of objects are they, stripe subtyping, stripe test, what default stripes exist | ||
- | |||
- | ===== Exception Handling ===== | ||
- | |||
- | explain: raise, try-catch and variants, first-class handlers, role of stripes, interface of an exception object |